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

1

22

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";
}
Heliocentric answered 24/10, 2012 at 22:19 Comment(0)
M
36

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";
}
Muck answered 24/10, 2012 at 22:42 Comment(1)
Ah I see, I misunderstood the purpose of that file.Heliocentric

© 2022 - 2024 — McMap. All rights reserved.