_Layout And _ViewStart And _ViewImports In ASP.NET MVC Core 3.0

Today in this article I will discuss about _Layout, ViewStart and ViewImports file in ASP.Net MVC Core 3.0. These are the files placed in views folder. We will understand all 3 files step by step.
Step 1
 
Start up your Visual Studio 2019. Choose ASP.NET Core Web Application and click on “Next”
 
_Layout And _ViewStart And _ViewImports In ASP.NET MVC Core 3.0
 
After clicking next another wizard will open. Under project name give a meaningful name for your project and click on create.
 
_Layout And _ViewStart And _ViewImports In ASP.NET MVC Core 3.0
 
That will open up another new wizard. Select ASP.Net Core 3.0 from dropdown if not select default. Choose empty template and click on create --  that will create your first ASP.Net Core Application.
 
_Layout And _ViewStart And _ViewImports In ASP.NET MVC Core 3.0
Step 2
 
Visual Studio 2019 will generate program and startup class. Now open startup file and configure ASP.Net MVC Core Application development. Under app.UseEndpoints method just Map endpoints.MapControllerRoute.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Threading.Tasks;  
  5. using Microsoft.AspNetCore.Builder;  
  6. using Microsoft.AspNetCore.Hosting;  
  7. using Microsoft.AspNetCore.Http;  
  8. using Microsoft.Extensions.DependencyInjection;  
  9. using Microsoft.Extensions.Hosting;  
  10.   
  11. namespace TagHelperMvcCore  
  12. {  
  13.     public class Startup  
  14.     {  
  15.         public void ConfigureServices(IServiceCollection services)  
  16.         {  
  17.             services.AddControllersWithViews();  
  18.         }  
  19.   
  20.         public void Configure(IApplicationBuilder app, IWebHostEnvironment env)  
  21.         {  
  22.             if (env.IsDevelopment())  
  23.             {  
  24.                 app.UseDeveloperExceptionPage();  
  25.             }  
  26.   
  27.             app.UseRouting();  
  28.             app.UseStaticFiles();  
  29.   
  30.             app.UseEndpoints(endpoints =>  
  31.             {  
  32.             endpoints.MapControllerRoute(  
  33.                     name: "default",  
  34.                     pattern: "{controller=Home}/{action=Index}/{id?}");  
  35.             });    
  36.         }  
  37.     }  
  38. }  

What is _Layout.cshtml? 

 
_Layout.cshtml
 
This is used for common layout in web pages; it is like master page. The layout typically includes common user interface elements such as the app header, navigation or menu elements, and footer. Common HTML structures such as scripts and stylesheets are also frequently used by many pages within an app. All of these shared elements may be defined in a layout file, which can then be referenced by any view used within the app. Layouts reduce duplicate code in views.
 
Step 3
 
Right click on project and create a folder called "Views" if you  don't already  have it.
 
Step 4
 
Right click on created views folder "Add" another folder under views folder name it shared folder. Now right click on shared folder and choose “Add New Item.” A window wizard will appear from window wizard; from that choose _Layout.cshtml razor file click on “Add” button. _Layout file will be added under shared folder.
 
_Layout And _ViewStart And _ViewImports In ASP.NET MVC Core 3.0
 
_Layout And _ViewStart And _ViewImports In ASP.NET MVC Core 3.0
 

What is _ViewStart.cshtml? 

 
_ViewStart.cshtml
 
It's a special file in ASP.NET Core MVC. The code in this file is executed before the code in an individual view is executed. Instead of setting the Layout property in each individual view, we can move that code into the _ViewStart.cshtml file. By setting the Layout property in _ViewStart.cshtml file maintaining our application becomes much easier. In the future, if we want to use a different layout file we just need to change the code in one place in _ViewStart.cshtml.
 
Step 5
 
Right click on view folder and _ViewStart.cshtml file similar to _Layout.cshtml file but this file should be placed outside shared folder.
 
_Layout And _ViewStart And _ViewImports In ASP.NET MVC Core 3.0
 

What is _ViewImports.cshtml?

 
_ViewImports.cshtml file is usually placed in the Views folder. It is used to include the common namespaces so we do not have to include them in every view that needs those namespaces.
 
Step 6
 
Right click on view folder and _ViewImports.cshtml file similar to _Layout.cshtml file but this file should be placed outside shared folder.
 
_Layout And _ViewStart And _ViewImports In ASP.NET MVC Core 3.0