Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specify different Layouts in the ASP.NET MVC 3 razor ViewStart file?

I would like to have 2 separate Layouts in my application. Let's say one is for the Public section of the website and the other is for the Member side.

For simplicity, let's say all the logic for each of these sites is wrapped neatly into 2 distinct controllers.

  • PublicController
  • StaffController

And that they each have a corresponding Layout for all the View under each.

  • _PublicLayout.cshtml
  • _StaffLayout.cshtml

How do I use the _ViewStart.cshtml file to specify that all Views / Actions under "Public" use the PublicLayout and everything under "Staff" uses the StaffLayout?

like image 204
Justin Avatar asked Mar 01 '11 22:03

Justin


People also ask

How do I add a layout in Razor view?

Right click the Views\HelloWorld folder and click Add, then click MVC 5 View Page with Layout (Razor). In the Specify Name for Item dialog box, enter Index, and then click OK. In the Select a Layout Page dialog, accept the default _Layout. cshtml and click OK.

Can we use multiple layout in MVC?

Yes, we can use multiple Layout in ASP.Net MVC application. By default, Visual Studio adds a Layout page in a shared folder which can be used by other View pages if required. In some cases, we can have the requirement to use multiple shared layout pages in MVC application.

Which file in an MVC application is used to specify the layout file for the application?

The _ViewImports. cshtml file for an ASP.NET Core MVC app is typically placed in the Pages (or Views) folder. A _ViewImports. cshtml file can be placed within any folder, in which case it will only be applied to pages or views within that folder and its subfolders.


1 Answers

You could put a _ViewStart.cshtml file inside the /Views/Public folder which would override the default one in the /Views folder and specify the desired layout:

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

By analogy you could put another _ViewStart.cshtml file inside the /Views/Staff folder with:

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

You could also specify which layout should be used when returning a view inside a controller action but that's per action:

return View("Index", "~/Views/Shared/_StaffLayout.cshtml", someViewModel); 

Yet another possibility is a custom action filter which would override the layout. As you can see many possibilities to achieve this. Up to you to choose which one fits best in your scenario.


UPDATE:

As requested in the comments section here's an example of an action filter which would choose a master page:

public class LayoutInjecterAttribute : ActionFilterAttribute {     private readonly string _masterName;     public LayoutInjecterAttribute(string masterName)     {         _masterName = masterName;     }      public override void OnActionExecuted(ActionExecutedContext filterContext)     {         base.OnActionExecuted(filterContext);         var result = filterContext.Result as ViewResult;         if (result != null)         {             result.MasterName = _masterName;         }     } } 

and then decorate a controller or an action with this custom attribute specifying the layout you want:

[LayoutInjecter("_PublicLayout")] public ActionResult Index() {     return View(); } 
like image 101
Darin Dimitrov Avatar answered Oct 30 '22 10:10

Darin Dimitrov