Asp.net-mvc – The following sections have been defined but have not been rendered for the layout page “~/Views/Shared/_Layout.cshtml”

asp.net-mvcasp.net-mvc-4mobilerazor

I know there are a few questions that have been answered but I didn't find something specific to my case.

I'm using the mobile capabilities of MVC4. So I created a _layout.mobile.cshtml and the corresponding views.

The error above happens when I go in with a mobile device. As you can see, it is trying to display the regular _layout.cshtml instead of the _layout.mobile.cshtml. So I'm assuming it is also trying to display the view (say Index.mobile.cshtm) which doesn't have the section in question. Basically it is mixing the regular layout with the mobile views.

This doesn't happen all the time. If I recycle the pool it works again for a while and then all of the sudden it goes back to having the error and it will continue until I recycle the pool again.

Has anyone seen this problem before that can shed some light?

Thanks
John

Best Answer

In the _ViewStart.cshtml available under the Views folder, change the Layout value to your custom layout. I think this may help.. (Make sure that you are returning View instead of partial view)

for example

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

In case if you want to change the layout for a specific page you can explicitly define it at the top of the page as a page directive.