Layouts And _Viewstart.cshtml In MVC

Introduction

In this blog, we will discuss about Layouts and what is the significance of _viewstart.cshtml in Razor views. The intention of this blog is to understand the way how _viewstart.cshtml works in MVC. The reason to write this blog is I faced an issue with _viewstart.cshtml,  when I was using HandleError attribute.

What is Layout?

We all know that layouts are like the master pages in Webforms. Simply, the common UI code, which can be used in many views can go into a common view called layout. Usually, we place all the layout pages in the shared folder.

How to use Layouts in MVC?

The following are the different ways to use the layouts:

  1. Using Layout in the View.
    1. @{  
    2. ViewBag.Title = "Index";  
    3. Layout = "~/Views/Shared/_Layout.cshtml";  
    4. }  
    If we declare the code above, given above in the Index.cshtml, this will be rendered along with the HTML content of the _Layout.cshtml. This is the way, we regularly follow.

  2. Assigning Layout in Controller's Action method.

    If we work with real the time Applications, we will come across the situations, like to use the different layouts , based on the action called.

    For example, I am looking for the list of the students. At this point, Layout would be different for admin and a Teacher but, both of them use same Razor View. Here, we will assign the Layout at an Action level.

    We can see assigning the Layout at Action Method in the following code snippet.
    If logged in as an admin:
    1. public List < Student > GetStudentList() {  
    2.     return View("StudentList""~/Views/Shared/AdminLayout.cshtml");  
    3. }  
    4. If Logged in as Teacher: public List < Student > GetStudentList() {  
    5.     return View("StudentList""~/Views/Shared/TeacherLayout.cshtml");  
    6. }  
  3. Declaring Layout at _Viewstart.cshtml.

    _Viewstate.cshtml plays an important and a tricky role in Razor views. It was introduced in MVC 3 along with Razor views. _Viewstart.cshtml is used to place common UI logic across the Views in the folder, where it is located. This means, the views in a single folder which is having _Viewstart.cshtml will be rendered along with it.

    For example: If we observe the views folder of an MVC project, we will see _Viewstart.cshtml in the folder.

    folder
  4. Thus, the views in Home and OpenAccess will be rendered along with the UI in _Viewstart.cshtml. We need not to declare anything in the views. This will be done automatically by the framework. 

Benefit

By doing so, we can change the layout at a single place only. Otherwise, we have to change the number of views.

Notes

  1. The inner layouts like a layout in Home/Index will be overriden by _Viewstart.cshtml.
  2. We can have n-number of _Viewstart.cshtml files in a project. But each should be placed in a folder. Alll the views in the folder will be effected with this.
  3. _Viewstart.cshtml will be called after the inner view.(Ex: Home/Index)

Finally My problem was

I used HandlerError attribute in a controller. If any exception is caught in the Action method, it should go to error.cshtml, but It was not called as error.cshtml view, placed in a folder, which has _Viewstart.cshtml. Thus, _Viewstart.cshtml will be called after the error.cshtml. _Viewstart.cshtml has the layout,  where I had a Null Reference exception. At last, I removed Layout from _Viewstart and able to see error.cshtml page.

Conclustion

In this blog, we have discussed about the different ways to place layouts in MVC and use of _Viewstart.cshtml. Finally, use _Viewstart.cshtml effectively.

Next Recommended Reading Creating Layout (Master) Pages In MVC