Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you define a layout for specific areas in ASP.Net MVC 4?

Tags:

I'm playing a little catch up here as I went straight from MVC2 to MVC4, so learning Razor and everything else all at once.

I'm using an admin area in this new application, and I noticed when I went to the controller in the admin area it rendered without any layout. I tried copying the _Layout.cshtml into the shared view folder of the area, but it still renders with no layout. I tried searching, but can't find any information as to how you set a layout to be used for an area.

I know I can do this on a specific view, but I want to set it once for the entire area

@{     ViewBag.Title = "Index";     Layout = "~/Views/Shared/_Layout.cshtml"; } 
like image 764
Jhorra Avatar asked Oct 24 '12 22:10

Jhorra


People also ask

What is layout in ASP.NET MVC how many layouts can you create in MVC?

Building Web Applications with ASP.NET Core 3 MVCLayouts are used in MVC to provide a consistent look and feel on all the pages of our application. It is the same as defining the Master Pages but MVC provides some more functionalities.

How do you specify a common layout for all views?

Right-click on "Views" and select "Add" >> "New Folder". Name the folder "Shared", this will create a "Shared" folder under the view. Right-click on the "Shared" folder and select "Add" >> "View…". Name the view as "_Layout" and select "Empty (without model)" as the template.


1 Answers

You have to have file _ViewStart.cshtml under folder Views in your area. This file would have something like this in it:

@{     Layout = Request.IsAjaxRequest() ? null : "~/Areas/Admin/Views/Shared/_Layout.cshtml"; } 
like image 173
Dmitry Efimenko Avatar answered Oct 06 '22 03:10

Dmitry Efimenko