I'm working on what should be a minor RazorViewEngine Extension, but having some difficulty with the second part of my approach.
Basically I have some controllers that are dedicated to some partial views, and would like to be able to place them in sub directories. Of course placing the models and controllers in sub directories is easy enough, but the views need to match as well. That's where it gets tricky.
Below you'll see my engine, and what I'm missing is how to get the name of the master controller. It seems like maybe I should be able to extend ControllerContext
somehow?
In the FileExists
override you can see I hard coded in a value to test, and that much works. But I don't just want administration. An example of the controller relationship - I have an administration controller, which loads a few partials, each with their own dedicated controller, for example User. So when I'm loading views for User, I'd like it to go to Views/Administration/User
instead of Views/User
.
public class PartialsViewEngine : RazorViewEngine
{
private static string[] NewPartialViewFormats = new[] {
"~/Views/%1/{1}/{0}.cshtml",
"~/Views/{1}/Partials/{0}.cshtml",
"~/Views/Shared/Partials/{0}.cshtml"
};
public PartialsViewEngine()
{
base.PartialViewLocationFormats = base.PartialViewLocationFormats.Union(NewPartialViewFormats).ToArray();
}
protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
{
string firstController = '????. //Trying to figure how to get this
var path = partialPath.Replace("%1", firstcontroller);
return base.CreatePartialView(controllerContext, path));
}
protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
{
var path = partialPath.Replace("%1", "Administration");
return base.FileExists(controllerContext, path);
}
}
How would I, with the least overhead possible to the controllers, obtain that value?
--- Update 1 ---
I noticed with this configuration resharper doesn't recognize the file paths even though it executes.