8

When ASP.NET MVC executes a page containing Razor it will first run the body eg the RenderBody method then it runs the code for the layout and weaves it together.

This is documented in this blog post:

System.Web.Mvc.RazorView.RenderView() System.Web.WebPages.WebPageBase.ExecutePageHierarchy() //non virtual version System.Web.WebPages.WebPageBase.PushContext()
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() //virtual version this.Execute() //Generated code from our View
System.Web.WebPages.WebPageBase.PopContext
RenderSurrounding(virtualPath, body); //Render Layout, which is similar to View's rendering process, essentially you can have nested Layout VerifyRenderdBodyOrSetions();

I want to add code to my views and layout that traces the actual logical position in the page.

Is there a way I can hook up a method to run just before RenderSurrounding and just after RenderBody finishes executing?

8
  • RenderBody is invoked by the layout (at least in the couple MVC projects I've worked on). You could possibly wrap RenderSurrounding in a helper (it if isn't already one) and add it before and after the call to RenderBody in the layout.
    – M.Babcock
    Feb 16, 2012 at 0:01
  • @M.Babcock ideally I am looking for a hook in the framework that involves no layout changes Feb 16, 2012 at 0:05
  • But your question says: I want to add code to my views and layout that traces the actual logical position in the page. So aren't you looking to change the layout anyway?
    – M.Babcock
    Feb 16, 2012 at 0:14
  • @M.Babcock doing a poor job explaining myself see: snipt.org/uNq8 I want to automatically hook up MarkLayoutStart and MarkLayoutBodyRendered without editing my master layout Feb 16, 2012 at 0:21
  • Have you thought about just writing your own base page that inherits from RazorView? See haacked.com/archive/2011/02/21/…
    – Paul Tyng
    Feb 16, 2012 at 4:21

1 Answer 1

8

You can override the ExecutePageHierarchy method on the page itself by creating a different base type for your pages, given a WebViewPage implementation like this:

public abstract class CustomViewPage<TModel> : WebViewPage<TModel>
{
    public override void ExecutePageHierarchy()
    {
        Output.Write("Before");
        base.ExecutePageHierarchy();
        Output.Write("After");
    }
}

You will get output just before and after the actual page's content (and thus inside the layout). I'm not sure if thats what you are looking for, are you trying to just write the scripts at </body> I imagine?

The main thing is to use this kind of a base type for just the main .cshtml (not any partials or on the layout) it will render those Output.Writes around the RenderBody output of the layout (well really, just inside).

You can set the base type per directory in the web.config.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.