I've made myself a modular framework with ninject for MVC.
Each module can register it's own routes and contains it's own views.
Module dir (dll location):
~/Modules/<module name>/
Module views sit inside:
<Module dir>/Views/
They are arranged exactly like a normal mvc app, IE a folder for each controller and a shared folder.
I want to render a view with a layout, however I want the layout location to be set by the core framework (so that i can change themes).
I have a view that has layout = _layout.cshtml
and when i run the app it returns:
The layout page "_Layout.cshtml" could not be found at the following path: "~/Modules/Module2/Views/Home/_Layout.cshtml".
The view that was called was here ~/Modules/Module2/Views/Home/Index.cshtml
. But I want it to look for the layout in another location without setting it in each view. Is there anyway I can do that in the core framework? Note i set it MasterLocationFormats to look in shared too, which it apparently does not (I tested that by placing a _layout.cshtml in there).
Custom View Engine:
public NinjectRazorViewEngine(): base()
{
ViewLocationFormats = new[] {
"~/Modules/%1/Views/{1}/{0}.cshtml",
"~/Modules/%1/Views/{1}/{0}.vbhtml",
"~/Modules/%1/Views/Shared/{0}.cshtml",
"~/Modules/%1/Views/Shared/{0}.vbhtml"
};
MasterLocationFormats = new[] {
"~/Modules/%1/Views/{1}/{0}.cshtml",
"~/Modules/%1/Views/{1}/{0}.vbhtml",
"~/Modules/%1/Views/Shared/{0}.cshtml",
"~/Modules/%1/Views/Shared/{0}.vbhtml",
};
PartialViewLocationFormats = new[] {
"~/Modules/%1/Views/{1}/{0}.cshtml",
"~/Modules/%1/Views/{1}/{0}.vbhtml",
"~/Modules/%1/Views/Shared/{0}.cshtml",
"~/Modules/%1/Views/Shared/{0}.vbhtml"
};
PartialViewLocationFormats = ViewLocationFormats;
AreaPartialViewLocationFormats = AreaViewLocationFormats;
}
protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
{
object moduleName;
if(controllerContext.RequestContext.RouteData.Values.TryGetValue("module",out moduleName))
return base.CreatePartialView(controllerContext, partialPath.Replace("%1", (string)moduleName));
return base.CreatePartialView(controllerContext, partialPath);
}
protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
{
object moduleName;
if (controllerContext.RequestContext.RouteData.Values.TryGetValue("module", out moduleName))
return base.CreateView(controllerContext, viewPath.Replace("%1", (string)moduleName), masterPath.Replace("%1", (string)moduleName));
return base.CreateView(controllerContext, viewPath, masterPath);
}
protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
{
object moduleName;
if (controllerContext.RequestContext.RouteData.Values.TryGetValue("module", out moduleName))
return base.FileExists(controllerContext, virtualPath.Replace("%1", (string)moduleName));
return base.FileExists(controllerContext, virtualPath);
}