Shared layouts not found with overridden View Paths (Location Formats) in ASP.NET MVC

I group my views, controllers and models. Structure

~/Controllers -- /_Shared -- -- /Views -- -- /Content -- -- /Scripts -- /Home -- -- /Models -- -- /Content -- -- /Scripts -- -- /Views -- -- HomeController.cs -- /Account -- -- /Models -- -- /Views ... 

Views and partial views work, but layouts (master views) do not work . When I specify the layout in the .cshtml file, for example:

 @{ Layout = "SimpleSharedLayout"; } 

I get this error: The "SimpleLayout" layout page was not found in the following path:

"~ / Controllers / Recording / Views / SimpleSharedLayout".

Asp.NET only looks for the layout in the current controller directory, and does not look at the shared folder * (which is in ~ / Controllers / _Shared / Views) *

Although this one works just fine.

 @Html.Partial("SharedPartialView") 

I need to specify layouts with full paths like

 @{ Layout = "~/Controllers/_Shared/Views/SimpleSharedLayout.cshtml"; } 

It is not difficult, but I am crazy because of the inability to make it work.

Using IIS Express, VS 2012, .NET 4.5

Do you have an idea of ​​what I am missing?

My view engine:

 public class AreaViewEngine : RazorViewEngine { public AreaViewEngine() { AreaViewLocationFormats = new[] { "~/Controllers/{1}/Views/{0}.cshtml", "~/Controllers/_Shared/Views/{0}.cshtml"}; ViewLocationFormats = AreaViewLocationFormats; AreaMasterLocationFormats = new[] { "~/Controllers/_Shared/Views/{0}.cshtml" }; MasterLocationFormats = AreaMasterLocationFormats; AreaPartialViewLocationFormats = new[] { "~/Controllers/_Shared/Views/{0}.cshtml", "~/Controllers/{1}/Views/{0}.cshtml"}; PartialViewLocationFormats = AreaPartialViewLocationFormats; } } 
+6
source share
2 answers

IMHO, you are fighting with framework agreements. It would be my recommendation to use the structure in the sense that it was intended for these scenarios, creating areas.

I know this is probably not the answer you want, but I feel like you described Areas to T.

+2
source

Try reloading ViewEngine and overwriting AreaMasterLocationFormats and MasterLocationFormats. Check out this other great answer, perhaps what you are looking for.

How to set default route (to zone) in MVC

Ignoring the title is not entirely about routes, but where and how viewEngine searches for files.

0
source

All Articles