views:

946

answers:

3

What is the ASP.NET MVC equivalent to "override void Render" from ASP.NET WebForms? Where are you opportunities to do some last-minute processing before the output is sent to the client?

For example, I wouldn't use this in production code, but illustrates cleaning up the <title> and <head> markup for an entire site when placed in the MasterPage of a WebForms app.

    protected override void Render(HtmlTextWriter writer)
    {
        System.IO.StringWriter sw = new System.IO.StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        base.Render(htw);
        htw.Close();
        string h = sw.ToString();
        string fhtml = h.Replace("<title>\r\n\t", "\n<title>")
                             .Replace("\r\n</title>", "</title>")
                             .Replace("</head>","\n</head>");
        // write the new html to the page
        writer.Write(fhtml);
    }

What is the best approach for playing with the final text rendering in ASP.NET MVC?

UPDATE:
So looking at the link (the chart) that Andrew Hare mentioned it looks you could do this in the View Engine. Can you bolt things onto or alter the way the default View Engine works or do you have to replace the whole thing?

+3  A: 

This isn't the way in which MVC works.

All your 'onprerender' code is done in the view. By the time you pass your data to the view from the controller you should have done all processing necessary to display the whole page.

The only slight exception to this is the model may still do a little processing while it is being probed by the view.

This may include setting variables required for partials to turn themselves on or off etc.

Ash
A: 

As far as I can see, the only thing you want to do in your Render()-method is to re-arrange some of the tags in the <head> section. This is already done correctly for you in MVC, unless you're using some home-built way of creating the <head>-section in your Masterpage.

I'd recommend aligning the tags exactly the way you want them directly in the html markup of your Master, and ensure that the ViewData["title"] or whatever you use to fill the tags with content to not start or end with line breaks.

Tomas Lycken
I was just using the <head> manipulation as an example. But not to get off on a tangent there are still some rough spots with MVC playing well in the IDE. I did on one project just have to bail on the "title PlaceHolder" default to the ViewData["title"] strategy.
tyndall
see comments to Jason on see http://stackoverflow.com/questions/529153 . Have the annoying underlining CSS issue now that I bailed on the PlaceHolder thing - http://stackoverflow.com/questions/524732
tyndall
And actually <title><%= ViewData["title"] %></title> in a ViewMasterPage will still put extra whitespace around your title.
tyndall
ok. think I solved that. Not sure if it has other side effects but if you remove the runat and id from your <head> tag this goes away. Still curious about the View Engine question.
tyndall