It turns out that the answer was to create a custom view engine (inherited from RazorViewEngine) for each domain that knows about the domain-specific folders:
public class Domain1ViewEngine() : RazorViewEngine
{
...
ViewLocationFormats = new[]
{
"~/Views/Domain1/{1}/{0}.cshtml",
"~/Views/Domain1/Shared/{0}.cshtml"
};
...
}
I then needed to override the FindView
and FindPartialView
methods so that it only attempted to find locate views if the request had come from the right domain:
public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
{
if ([THIS IS NOT THE RIGHT DOMAIN])
{
return new ViewEngineResult(new string[] { });
}
return base.FindView(controllerContext, viewName, masterName, useCache);
}
To complete the process I registered the view engine in Global.asax.cs
in the usual way:
protected void Application_Start()
{
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new Domain1ViewEngine());
ViewEngines.Engines.Add(new RazorViewEngine());
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…