Missing Layout in ASP.NET MVC Area’s Views

In ASP.NET MVC, it’s quite common to expect that the site layout defined in ~\Views\Shared\_Layout.cshtml can be applied in every view in the site. However, when you create a area’s view in ASP.NET MVC, you may found that the site layout defined in ~\Views\Shared\_Layout.cshtml cannot be rendered in the area’s view.

There are 3 ways to resolve this problem:
1. Copy the ~\Views\Shared\_Layout.cshtml file to the ~\Views\Shared\ folder in each area
2. Copy the ~\Views\_ViewStart.cshtml file to the ~\Views\ folder in each area
3. (Recommended) Move the ~\Views\_ViewStart.cshtml file to the root folder ~\ and modify the ~\web.config file

Method 3 is recommended because it is the only “Do once solve all” solution. Don’t need to do any extra things when you create a new area. Don’t Repeat Yourself (DRY).

1. Copy and paste system.web.webPages.razor config section group and system.web.webPages.razor section in ~\Views\web.config to ~\web.config

<configSections>
	<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
		<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
		<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
	</sectionGroup>
</configSections>
<system.web.webPages.razor>
	<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
	<pages pageBaseType="System.Web.Mvc.WebViewPage">
		<namespaces>
			<add namespace="System.Web.Mvc" />
			<add namespace="System.Web.Mvc.Ajax" />
			<add namespace="System.Web.Mvc.Html" />
			<add namespace="System.Web.Optimization" />
			<add namespace="System.Web.Routing" />
		</namespaces>
	</pages>
</system.web.webPages.razor>

2. Move the ~\Views\_ViewStart.cshtml file to the root folder ~\

3. Make sure the layout file defined in ~\_ViewStart.cshtml is point to ~\Views\Shared\_Layout.cshtml

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

4. Completed!!! The layout defined in ~\Views\Shared\_Layout.cshtml can now be rendered in the area’s view.

References
+ Sharing a single _ViewStart Across Areas in ASP.NET MVC
+ Asp.Net MVC 3 Areas and _ViewStart.cshtml Scope

Advertisements

2 thoughts on “Missing Layout in ASP.NET MVC Area’s Views

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s