Introduction
The MVC _ViewStart.cshtml
file has almost magical properties since it will be loaded before any other View file. However, I didn't know what it was that loaded it and I was curious. This article investigates what _ViewStart.cshtml
does and how it does it so you can understand the MVC framework a little better.
Background
While researching information for my first article ( ASP.NET MVC : Build Your Custom Blog Engine (CMS) - Part 1 of 2(Own ASP.NET MVC) -- opens in new tab ) I wanted to know what exact part of the MVC framework loaded the _ViewStart.cshtml
so I could explain it properly. That lead me to examining the RazorViewEngine()
with ILSpy and resulted in this article.
First I'll, show you a little magic that occurs within the framework with this file, then I'll show you the MVC code using ILSpy.
First let's just take a look at how the template project seems to magically load the layout file.
Magic Loading of Layout
First of all, start up a new ASP.NET MVC project using the Visual Studio project template.
Choose the ASP.NET Web Application and click the [Add] button.
Next, choose the MVC project type.
Click the [Change Authentication] button to remove the option. We don't need it for our sample code.
Set the option to No Authentication and click the [OK} button.
Visual Studio will create your MVC project from its template.
As soon as the project is created, go ahead and build and run the project. Hint: CTRL-F5
will build and run for you.
The IIS dev web server will start and the MVC application will start in your default browser and it will look something like the following:
Check Out The NavBar
If you click on each of those menu items at the top of the page you will see that no matter which page you navigate to, you will still see the NavBar. That seems reasonable, but what does that?
The code for that NavBar is found in the \Views\Shared\_Layout.cshtml
file.
But, how does that file get applied to every one of your Views?
None of your views actually include the _Layout.cshtml
file so how does that work?
Examine the Views Folder
If you look at Solution Explorer and examine the Views Folder you will see that there is a file in root \Views folder named _ViewStart.cshtml
.
Magic File Has No Known Reference In the Project
_ViewStart.cshtml
is the magic file which is not referred to anywhere else in your project or project files but somehow does work in your project.
Here is the entire code listing contained in the _ViewStart.cshtml
file:
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
That's it. And I guess that makes sense, because you can see that it is telling the RazorViewEngine that the Layout should be set to the file : ~/Views/Shared/_Layout.cshtml
That makes sense because that file is loaded and it is the one which displays the NavBar.
The Question : How Does _ViewStart.cshtml Ever Get Loaded?
The point here is that since nothing seems to reference _ViewStart.cshtml
, how does it ever get loaded itself?
As a test, let's Exclude this file from the project. Right-click on the file in Visual Studio and choose the Exclude from Project menu item:
After you do, the file will disappear.
Quiz: What Will Happen Now?
What do you suppose will happen when we run the project again.
Go ahead and run the project again. CTRL-F5
Quiz Answer: No Difference
If you do that, you'll find that there is no difference in the project. It still loads _Layout.cshtml and the NavBar is still there. Why?
Shocking Answer
Because the file is excluded from the project but the file still exists on disk and the RazorViewEngine
simply drills down through your project to find that file by name and when it does it follows it's directives.
Prove It
Okay, I'll prove it.
Go to the place on your disk (using Windows Explorer) where your project is, and then open the \Views folder and you'll see the _ViewStart.cshtml
file.
Now, rename the file to anything else. Simply add a letter X to the beginning, for example so it is now named X_ViewStart.cshtml
.
Go back to studio and run the project again : CTRL-F5
Now the page looks something like the following -- no styles, because _Layout.cshtml is no longer loaded and the NavBar is gone.
This is all because the RazorViewEngine drills down through your project folders, looking for this specially named file.
How can you see the code where the RazorViewEngine does this?
Finding the Code In the MVC Library
To find out where the code might be hiding in ASP.NET MVC framework, I googled the following text:
razorviewengine dll
The first result was a link to the RazorViewEngine class (MSDN opens in new tab)
That page displayed the following information about RazorViewEngine.
System.Object
System.Web.Mvc.VirtualPathProviderViewEngine
System.Web.Mvc.BuildManagerViewEngine
System.Web.Mvc.RazorViewEngine
I then went to my current MVC project via Windows Explorer and traversed down into the bin directory. There I found the System.Web.Mvc.dll
and I dropped it into the ILSpy interface.
Get Your Free Copy of ILSpy
You can get ILSpy at : http://ilspy.net/ - opens in new tab
After that I did a bit of searching within ILSpy and the MVC dll for the text : _ViewStart.
Then I found it:
You can also see where _ViewStart.cshtml
string is set in the code at:
Points of Interest
I have not seen this documented in this way anywhere else on the web. I think it is interesting, because we have exposed how the magic works and it helps us understand the entire MVC framework a bit better. I hope you've enjoyed this too.
NOTE: I haven't included any download source simply because it is just the ASP.NET MVC template project that we used with very little modification.
History
First edition of this article: 2015-01-25