Learn About Layout Page In ASP.NET MVC

As a developer, you always want to write that type of code or design which can be reusable again and again. In term of look and design of any website, we use Layout page to define the consistent look and feel across the whole application. There is some requirement to create a standard look and feel in which some data always show on every page like it can be Menu Bar, Logo of the application, Header, Footer, etc.

In that scenario, we always prefer to create a layout page in ASP.NET MVC that can be used with different View with custom and consistent look and feel.

So, basically Layout View is a part of UI where we define our common component which will be accessible throughout the application. It helps us and there is no need to rewrite the same code again and again.

Footer

As we know in the web application dynamic part always change but the static part like header, footer, sidebar are always same. So here Layout View comes in picture.

When you create a default ASP.NET MVC application then it creates default _Layout.cshtml page for the application. One more view exists and it is _ViewStart.cshtml. Here we define the default layout page for all Views if layout page is not defined.

ViewStart

See the default code for _Layout.cshtml and _ViewStart.cshtml page. You can see header and footer are defined, which will be available on every page.

_Layout.cshtml

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <meta charset="utf-8" />  
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">  
  6.     <title>@ViewBag.Title - My ASP.NET Application</title>  
  7.     @Styles.Render("~/Content/css")  
  8.     @Scripts.Render("~/bundles/modernizr")  
  9.   
  10. </head>  
  11. <body>  
  12.     <div class="navbar navbar-inverse navbar-fixed-top">  
  13.         <div class="container">  
  14.             <div class="navbar-header">  
  15.                 <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">  
  16.                     <span class="icon-bar"></span>  
  17.                     <span class="icon-bar"></span>  
  18.                     <span class="icon-bar"></span>  
  19.                 </button>  
  20.                 @Html.ActionLink("Application name""Index""Home"new { area = "" }, new { @class = "navbar-brand" })  
  21.             </div>  
  22.             <div class="navbar-collapse collapse">  
  23.                 <ul class="nav navbar-nav">  
  24.                     <li>@Html.ActionLink("Home""Index""Home")</li>  
  25.                     <li>@Html.ActionLink("About""About""Home")</li>  
  26.                     <li>@Html.ActionLink("Contact""Contact""Home")</li>  
  27.                 </ul>  
  28.                 @Html.Partial("_LoginPartial")  
  29.             </div>  
  30.         </div>  
  31.     </div>  
  32.     <div class="container body-content">  
  33.         @RenderBody()  
  34.         <hr />  
  35.         <footer>  
  36.             <p>© @DateTime.Now.Year - My ASP.NET Application</p>  
  37.         </footer>  
  38.     </div>  
  39.   
  40.     @Scripts.Render("~/bundles/jquery")  
  41.     @Scripts.Render("~/bundles/bootstrap")  
  42.     @RenderSection("scripts", required: false)  
  43. </body>  
  44. </html>  
_ViewStart.cshtml
  1. @{  
  2.    Layout = "~/Views/Shared/_Layout.cshtml";  
  3. }  
run

Create Multiple Layout Page in Same Application

If it is required to add the multiple layouts with different page in same application then you can add multiple layout pages in the same application. But at the time of calling the layout page, you need to define the layout page name inside the View.

To add a new Layout Page, simply add a partial view inside the Shared folder with a name and bind it with any particular View or you can also directly add the layout page.

Create Multiple Layout page

Made some changes in the newly added layout page.

_MyLayout.cshtml
  1. <!DOCTYPE html>  
  2.   
  3. <html>  
  4. <head>  
  5.     <meta name="viewport" content="width=device-width" />  
  6.     <title>@ViewBag.Title</title>  
  7. </head>  
  8. <body>  
  9.     <div>  
  10.           
  11.         This is my custom layout page  
  12.   
  13.   
  14.         @RenderBody()  
  15.     </div>  
  16. </body>  
  17. </html>  
MyLayout

Here is the output.

output

Thanks for reading this article, hope you enjoyed it.

 

Up Next
    Ebook Download
    View all
    Learn
    View all