- The _ViewStart.cshtml page is a special view page containing the statement declaration to include the Layout page.
- Instead of declaring the Layout page in every view page, we can use the _ViewStart page.
- When a View Page Start is running, the “_ViewStart.cshtml” page will assign the Layout page for it.
- So the application will be less maintainable.
How to create _a Viewstart page?
Add a View named _ViewStart to the project. (Remember the name “_ViewStart” is important.).

Note. If it already exists, do not add any more.

Let’s see its effect. There is no statement to include “Layout.cshtml”.

Run the application and watch the output.

This is because the Layout file is included on the “_ViewStart.cshtml“ page.

Let’s comment out the code line in the _ViewStart page run the application again and watch the output.

Run the application.

Note. You can also explicitly add the Layout.cshtml page to the Target page.

The Benefit of the _ViewStart.cshtml page
- Instead of declaring the Layout page individually on every page, we can include it in one place only.
- So the application became maintainable.
- You can still add another page as your Layout page to individual pages, regardless of your _ViewStart page.
So remove the Layout page and the declaration from individual pages and put it in the “_ViewStart.cshtml” page only.

Watch the difference

Run the application



ARIF ANSARIPosted Sep 26, 2022, 12:19 PM EDT
Very nice article..
Saunak SahaPosted Feb 19, 2020, 6:47 AM EST
Good article
Pravin LalgePosted Dec 6, 2018, 7:02 PM EST
Good article
Nigel FernandesPosted Aug 9, 2017, 6:34 PM EDT
Thanks it was useful
Pintoo YadavPosted Sep 10, 2015, 10:41 PM EDT
very Good
Shridhar SharmaPosted Sep 1, 2015, 7:05 AM EDT
good one .
Sibeesh VenuTOP 100Posted Sep 1, 2015, 3:29 AM EDT
Nice Share
Karthikeyan KPosted Aug 31, 2015, 7:21 PM EDT
Good one sir...Thanks for share
Pankaj Kumar ChoudharyTOP 100Posted Aug 31, 2015, 4:01 PM EDT
Very Well explain Sir.......
Mohammed IbrahimPosted Aug 31, 2015, 2:50 PM EDT
nice
Rajeesh MenothPosted Aug 31, 2015, 11:01 AM EDT
Nice One