Introduction
This article shows how to set a layout link once for all views in MVC.
Step 1
Create a MVC project from the "Empty" template.
Right-click on "Controllers" and select "Add" >> "Controller...".

Step 2
Select "MVC 5 Controller - Empty" to add an empty controller.
Click on the "Add" button.

Step 3
Name the controller as in the following:

Step 4
Now we need to create a view.
Right-click on "Index" and select "Add View...".

Step 5
Name the view and select "Empty (without model)" as the template. Click on the "Add" button.
Add content to the body part.

Step 7
Right-click on "Views" and select "Add" >> "New Folder".

Step 8
Name the folder "Shared", this will create a "Shared" folder under the view.
Right-click on the "Shared" folder and select "Add" >> "View…".

Name the view as "_Layout" and select "Empty (without model)" as the template.

Step 11
Design your desired layout in _Layout.cshtml.

Step 12
Right-click on "Views" and select "Add" >> "View…". 
Step 13
Name the view as "_ViewStart" and it will add _ViewStart.cshtml under Views.

Step 14
In a previous article we saw that _Layout.cshtml link is set in each view. But rather than setting in each view we set the _Layout.cshtml link in _ViewStart.cshtml under the views. And this layout will be implemented in all views. So we need not set a layout link in each view.
Run the project and you can see the Index view is rendered with layout.


Surat SureshnagPosted Sep 21, 2017, 11:02 PM EDT
Hi Akshay, Your Day 6 article is not rendering and it is redirecting to home page. Please check it once and update. Thanks!!
Former memberPosted Aug 27, 2015, 11:51 PM EDT
not being able to move to your part 6 page.....url is wrong there. check and fix plzz
phuong LePosted Oct 27, 2014, 3:12 PM EDT
Thanks Akshay Patel
Akshay PatelTOP 100Posted Oct 27, 2014, 2:43 AM EDT
Hi phuong, You can not change its name as it is convention that we have to follow in MVC. _ViewStart.chstml is a special View page which is automatically appended at the beginning of all Views in the view folder.
phuong LePosted Oct 27, 2014, 1:51 AM EDT
Hi Akshay Patel, Thanks for "Layout For All Views Sample in MVC, Day 7" it very helpful for me. I have a question. I don't know how mvc set view "_ViewStart"? I try change name "_ViewName1"... It's wrong. I don't know where MVC _VIewStart. Could you helps me? Thanks