0

I have a MVC application that exist at the moment using a _MainLayoutPage for its Master Page.

I want to create another Master Page for a different purpose. I will be creating a new controller as well.

How can I do this?

Renaissance
  • 798
  • 5
  • 15
Baba
  • 2,059
  • 8
  • 48
  • 81
  • 2
    http://stackoverflow.com/questions/5161380/how-do-i-specify-different-layouts-in-the-asp-net-mvc-3-razor-viewstart-file – Sirwan Afifi Dec 10 '13 at 17:26

2 Answers2

2

The simplest way is in your Action Method, set a Viewbag property for your Layout

public ActionResult Index()
{
  ViewBag.Layout= "~/Views/Shared/layout2.cshtml";

In your View, set the layout property

@{
    Layout = @ViewBag.Layout;
}
Uri Mikhli
  • 690
  • 6
  • 19
0

In _ViewStart.cshtml, put this:

    @{
        try {
            Layout = "~/Views/" + ViewContext.RouteData.Values["controller"] + "/_Layout.cshtml";
        }
        catch {
            Layout = "~/Views/Shared/_Layout.cshtml";
        }
    }

And then you can put a controller specific _Layout.cshtml in your controller folders, like

  • ~/Views/User/_Layout.cshtml for a controller named UserController
  • ~/Views/Account/_Layout.cshtml for a controller named AccountController

And because of the try/catch, it will fall back to the '~/Views/Shared/_Layout.cshtml' layout if one is not defined for a specific controller.

John Gibb
  • 10,603
  • 2
  • 37
  • 48