Multiple Layout Pages In ASP.NET Core MVC

The layout page shares the common design across all pages. There are many methods which are listed below to change the layout page dynamically in ASP.NET Core MVC.
  • While Adding View Page, Assign Layout Page .
  • Using View Start Page
I hope you have understood about the layout page from the preceding brief summary. Now let's implement it practically.
  • Start then All Programs and select "Microsoft Visual Studio 2019".
  • Once the Visual Studio Opens, Then click on Continue Without Code.
  • Then Go to Visual Studio Menu, click on File => New Project then choose ASP.NET Core Web App (Model-View-Controller) Project Template.
  • Then define the project name, location of the project, Target Framework, then click on the create button.
The preceding steps will create the ASP.NET Core MVC application.

Step 2:  Add user and admin controller controller.

Right click on the Controller folder in the created ASP.NET Core MVC application and add the two controller classes with names UserController and AdminController as follows.

The preceding two controller classes are added into the project which are User and Admin and create the following action methods in respective controller class.


public class UserController : Controller  
        public IActionResult Login()  
            //write logic here  
            return View();  

public class AdminController : Controller  
       public IActionResult AddRole()  
           //write logic here  
           return View();  
For preceding two controllers we will use two different layout pages.

Step 3: Add Views and Layout pages

We can decide which layout page to be used while adding the view. Let us follow the following steps to add the layout page with view. Click on the View folder of the created ASP.NET Core MVC application as,

As shown in the preceding image, specify the view name and check the use layout page option and click the adding button, then the following default layout page will be added into the solution explorer. Now let's add another layout page named admin as in the following. Click on solution explorer and add the layout page as follows:

Now click on add button, then two layout pages are added under shared folder which are AdminLayoutPage and Layout.

Step 4: Set layout pages to view

We have created view and layout pages. Now let us assign layout pages to the views. There are many ways to assign layout page to the view which are listed as in the following:
  • Using wizard
  • Using ViewStart page
  • Using view method
Using wizard

You can use wizard to set the layout page while adding the view, steps are as follows:
  • Right click on view folder and select view template as,

Specify the view name and check on Use a layout page and click on browse button. The following window will appear,

Now choose layout page from preceding available Layout pages and click on ok button. The layout page will look like as follows,

Now click on add button then layout page reference added into the view page as,

So whenever you will add through wizard or manually the layout page reference need to be set in every view page where the layout page is needed.

Using ViewStart page

Adding reference of layout page in every page is very difficult and repetitive of code. Let us consider I have one controller which as twenty plus action method then each twenty views we need to add reference of layout page. Assume another requirement we need to set layout page according to condition basic or controller basic then we need to use Viewstart page.

Lets open the ViewStart.cshtm page and write the following code,

    string CurrentReq = Convert.ToString(Context.Request.HttpContext.Request.RouteValues["Controller"]);
dynamic Layout; switch (CurrentReq)
{ case "User": Layout = "~/Views/Shared/_Layout.cshtml"; break; default: //Admin layout Layout = "~/Views/Shared/_AdminLayoutPage.cshtml"; break; } }
Now run the application, the Login view will look like as follows in which we have used Layout page,

Now run AddRole view, Then the output will look like the following,

I hope from all the preceding examples, you have learned how to work with multiple layout pages in ASP.NET Core MVC.

  • Apply proper validation before using it in your project.

I hope this article is useful for all readers. If you have any suggestions, then please mention it in the comment section.

Related Tutorials

How To Create an ASP.NET MVC Application

Post a Comment

Protected by Copyscape
Copyright © Compilemode