Reputation: 1728
I've read somewhere that in ASP.NET MVC, the view gets "loaded" before the layout (the view logic gets applied before the layout logic). So I was wondering what's the real answer here, and is there a way to check this?
Upvotes: 5
Views: 2951
Reputation: 457
I would say that the layout logic gets loaded "first." For example, I have logic in my _layout that states:
@if (User.isAdmin)
{
@Html.ActionLink("Settings", "Index","Admin")
}
Saying that if the users role is admin then allow them to view/click the settings button. So the layout logic would have to load "first."
Upvotes: -2
Reputation: 54618
If you mean what view get rendered in which order you can simply add debug statements to the views to find out. For example:
_Layout.cshtml
@{ System.Diagnostics.Debug.WriteLine("Pre-RenderBody()");}
@RenderBody()
@{ System.Diagnostics.Debug.WriteLine("Post-RenderBody()");}
index.cshtml
@{ System.Diagnostics.Debug.WriteLine("index.cshtml:start");}
<div>Home Page</div>
@{ System.Diagnostics.Debug.WriteLine("index.cshtml:end");}
Show ouput from: [Debug]:
index.cshtml:start
index.cshtml:end
Pre-RenderBody()
Post-RenderBody()
So the index.cshtml is executed first. Then the layout, and the content is inserted (not really the right term as it's actually appended to the stream/string/stringbuilder prior to everything after @RenderBody()
).
Upvotes: 6