What is ViewStart in MVC?

 Posted by Kmandapalli on 1/8/2014 | Category: ASP.NET MVC Interview questions | Views: 24096 | Points: 40

For group of views that all use the same layout, this can get a bit redundant and harder to maintain.
The _ViewStart.cshtml page can be used to remove this redundancy. The code within this file
is executed before the code in any view placed in the same directory. This fi le is also recursively applied to any view within a subdirectory.
When we create a default ASP.NET MVC project, we find there is already a _ViewStart
.cshtml fi le in the Views directory. It specifi es a default layout:
Layout = "~/Views/Shared/_Layout.cshtml";
Because this code runs before any view, a view can override the Layout property and choose a different one. If a set of views shares common settings, the _ViewStart.cshtml file is a useful place to consolidate these common view settings. If any view needs to override any of the common settings, the view can set those values to another value.

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response