Holi Sale. Get upto 40% OFF on Job-oriented Training! Offer Ending in
D
H
M
S
Get Now
Browse Tutorials

02 Intermediate

Different ways of rendering layouts in Asp.Net MVC

Different ways of rendering layouts in Asp.Net MVC

05 Mar 2024
Intermediate
250K Views
2 min read

In Asp.Net MVC, Layouts are like Master Pages in Asp.Net WebForms. These help us maintain a consistent look and feel across all the views within your Asp.Net MVC application. Like Master Pages, Layout may contain common CSS, and jQuery files across the multiple Views and one or more placeholders for which Views provide content. Before it, you can go through types of Layouts likeLayouts, RenderBody, RenderSection and RenderPage in ASP.NET MVC.

In Asp.Net MVC, at the application level, we have a _ViewStart file within the Views folder for defining the default Layout page for your Asp.Net MVC application. In this article, I am going to expose the different ways to apply layout pages for your application. Suppose we have to render the layouts as shown in the fig. by using various ways.

Method 1: Control Layouts rendering by using the _ViewStart file in the root directory of the Views folder

We can change the default rendering of layouts within the _ViewStart file by using the below code:

@{
 var controller = HttpContext.Current.Request.RequestContext.RouteData.Values["Controller"].ToString();

 string layout = "";
 if (controller == "Admin")
 {
 layout = "~/Views/Shared/_AdminLayout.cshtml";
 }
 else
 {
 layout = "~/Views/Shared/_Layout.cshtml";
 }
 
 Layout = layout;
}

Method 2: Return Layout from ActionResult

We can also override the default layout rendering by returning the layout from the ActionResult by using the below code:

public ActionResult Index()
{
 RegisterModel model = new RegisterModel();
 //TO DO:
 return View("Index", "_AdminLayout", model);
}

Method 3: Define the Layout within each view on the top

We can also override the default layout rendering by defining the layout on the view by using the below code:

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

Method 4: Adding _ViewStart file in each of the directories

We can also set the default layout for a particular directory by putting the _ViewStart file in each of the directories with the required Layout information as shown below:

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

I hope you will enjoy the tips while rendering layouts in your MVC application. I would like to have feedback from my blog readers. Your valuable feedback, questions, or comments about this article are always welcome.

FAQs

Q1. What is to be rendered in the view in MVC framework?

The views are ASP.NET Web pages that are rendered by the MVC framework.

Q2. Which view contains common parts of UI?

 It introduced a Layout view that contains these common UI portions so that we don't have to write the same code on every page

Q3. Can we create multiple layout in MVC?

MVC allows the use of multiple layouts in a single application.
Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this