Home » ASP » ASP.MVC » What is the role of the Viewstart file in an Asp.Net MVC?

What is the role of the Viewstart file in an Asp.Net MVC?

Since version 3.0 of the Asp.Net MVC framework, you can use the ViewStart file inside the View folder. You can use it inside your area (inside the views of your area) and also have multiple ViewStart file within the View folder if your views contain sub directory.

The ViewStart files are read from the root of the view folder to the more deep folder of the view called. So, if you are using an area which have several folder for different view, you can have a ViewStart inside the “Views” folder and one inside the “Views/Folder1”. Asp.Net MVC will read the ViewStart from the “Views” folder and then the one inside “Views/Folder1”.

An other characteristic of the ViewStart is that this one is called “_ViewStart.cshtml”. It must start with an underscore and use the .cshtml extension.

The role is to avoid to repeat some code that must be the same between views. For example, the layout for the view (the master page). Instead of defining the the top of each view what layout to use, you can simply define a layout at the root of your views folder a file _ViewStart.cshtml and put :

@{
    Layout = "~/Views/Shared/_MasterPage.cshtml";
}

If you need a different layout for a specific view, simply set the layout inside the view or inside a ViewStart inside the folder of the view. Since the order of read is from the root of the Views folder to its subdirectory, this last ViewStart will set the Layout property and override the one defined previously.

If you are getting an exception saying something about cast like the following error it’s because you are using the ViewStart outside the Views folder.

Unable to cast object of type ‘ASP._Page_Areas_Admin__ViewStart_cshtml’ to type ‘System.Web.WebPages.StartPage

From there, you are limited about what you can do. But, you have access to the ViewContext.TempData which let you set variable values which can be validated in further level. For example, you can set something at the root of the ViewStart and redefine, or add if it’s a list, data in a further level. At the end, in the Layout you could read the TempData and act in consequence.

In real life, I have used once the ViewStart with a TempData to set Javascript file name that needed to be loaded but not for 2 folders. In those ViewStart, the entry was removed. In the master page, the TempData was read and the Javascript loaded. You can get more information about this real life example here.

From here, you should be able to understand that the ViewStart is a way to don’t repeat yourself between views. You should also understand that not only the ViewStart inside the view folder of the view is executed but all ViewStart in the path of your view from the root to your view is executed.

If you like my article, think to buy my annual book, professionally edited by a proofreader. directly from me or on Amazon. I also wrote a TypeScript book called Holistic TypeScript

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.