_Layout & _ViewStart Significance In Asp.Net Core MVC

_Layout & _ViewStart Significance in Asp.Net Core By Sagar Jaybhay

LayoutView In MVC

MVC Architecture

In today’s web application we have above fig-like front-page architecture and in this, we have command left menu, header and footer are common. When you hit any menu item or button on the website only middle-pane which is a dynamic view is changed all things remain the same.

If you made all these same functionalities to every page which you have then it becomes tedious to manage and time-consuming so in asp.net core MVC have the common functionality on single location so that you will able to manage this easily.

For the layout, view add-in shared folder under view folders. It is like a master page in asp.net web forms.

Layout file starts with _Layout.cshtml by default.

Razor Layout
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
</head>
<body>
    <div>
        @RenderBody()
    </div>
</body>
</html>

We can have multiple layout files in a project like for admin, general users.

In this file, we have a common element like head, body, and footer.

RenderBody():

This is method in layout file in asp.net core MVC and which call specific individual view. Remember layout view is not good to pass a big amount of data. This is the actual location where view specific content is plugged in.

If any view want to refer layout page then we need to add below code in that view.

@{ 
    Layout = "~/Views/Shared/_Layout.cshtml";

}

It is one way to use a layout view for your view.

Use of layout view:

  1. Consistent look and feel is achieved
  2. It is similar to a master page in web-forms
  3. The default name is _Layout.cshtml
  4. It is present inside Views/Shared folder
  5. An application may have more that one layout page for a website.

RenderSection

This method adds a script section to the layout view. With respect to each view. Means whatever the scripts you included in individual view they are called in page when layout view calls that view.

@RenderSection('Scripts')

It has one overloaded version which takes Boolean an as a flag. If we don’t use overloaded version then our views need to have the script section else it will throw an error.

@RenderSection("Scripts",required:false)

It Means that script section is optional and if not present in respected view it will not throw an error. The second way to check section is enable or not. IsSectionDefined(“Scripts”) this returns a Boolean true or false and by that way, you will know script section is declared or not.

_ViewStart

If we want to use a layout page in our view we have to.

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

Add this tag in each view. SO think in this way if we have 100 views then it very tedious and time-consuming and repeating work to copy-paste this line in every view and in the future view need to change. We use _ViewStart.cshtml.

@{
    Layout = "_Layout";
}

This _ViewStart.cshtml also one can add it into a sub-folder of views and it Is override in a hierarchical manner.

ViewStart.cshtml

By using this you can add conditional layout page selection criteria here and based on criteria we can select different layout files.

@if(User.IsInRole("Admin"))
{
    Layout = "_Layout";
}else
{
    Layout = "_Layout_other";
}

Points to remember:

  1. Code in view start executed before the individual view executed
  2. Common code to all views can place here
  3. Code redundancy reduces and maintainability increases
  4. It is hierarchical.
ViewStart

ViewImports

This is a new feature added in asp.net core.When we need some model or library class in view then we need to add the complete namespace of that class. Like below screen.

ViewImport

But when you use _viewImport.cshtml file then common namespace which you required in your view can add here and then no need of fully qualified namespace to use in your view.It is also hierarchical like _ViewStart.cshtml

use_of_view_imports

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

Related posts