With some ideas from me and a friend of mine and a link about overriding RazorViewEngine
I finally got what I wanted working exactly how I was expecting it to:
- I created a folder named
ViewsBase
in the main project. I rewrote
RazorViewEngine
this way: I only changed the place that was needed for me, leaving everything else like I found in the constructor ofRazorViewEngine
:public class MyCustomViewEngine : RazorViewEngine { public MyCustomViewEngine() { ...
ViewLocationFormats = new[] { "~/Views/{1}/{0}.cshtml", "~/Views/{1}/{0}.vbhtml", "~/Views/Shared/{0}.cshtml", "~/Views/Shared/{0}.vbhtml", "~/ViewsBase/{1}/{0}.cshtml", "~/ViewsBase/{1}/{0}.vbhtml" }; ...
(I find it rather disturbing how I am unable to format the code properly. Can someone give me a hand please?)
and in Global.asax
of the main project I added:
ViewEngines.Engines.Clear();
var ourViewEngine = new
ViewEngines.Engines.Add(ourViewEngine);
AreaRegistration.RegisterAllAreas();
...
I added a post-build event command:
xcopy /s "$(ProjectDir)Views\*.*" /Y "$(SolutionDir)$(SolutionName)\ViewsBase\"
It started looking for the views in the expected order
- I added a ViewStart file to the base project to make it render the Layout too.