What is the use of ViewStart in ASP.NET MVC?

 Posted by Sheonarayan on 10/24/2013 | Category: ASP.NET MVC Interview questions | Views: 18506 | Points: 40
Answer:

In the default template of ASP.NET MVC, we get _ViewStart.cshtml page that is used to almost similar to MasterPage in ASP.NET Web Form or like a layout template.

The code of ViewStart executes before the actual view code executes. By default, _ViewStart template is applied to all view files of this or any sub directory under this directory.

The _ViewStart.cshtml file contains following code.

@{

Layout = "~/Views/Shared/_Layout.cshtml";
}

As this code runs before any view code so we have flexibility to specify a different Layout for a particular view.

Because this code runs before any view, a view can override the Layout property and choose a different
one.


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response