Reputation: 7308
I am trying to implement a custom view engine with Razor. The goal is if the view is in a sub folder to use that view instead.
I have my view engine derived from the RazorViewEngine
public class RazorViewFactory : RazorViewEngine
{
public RazorViewFactory()
{
string TenantID = ConfigurationManager.AppSettings["TenantID"];
if (TenantID != null)
{
MasterLocationFormats = new[] {
"~/Views/Shared/{0}.cshtml"
};
ViewLocationFormats = new[]{
"~/Tenant/" + TenantID + "/Views/{1}/{0}.cshtml",
"~/Tenant/" + TenantID + "/Views/Shared/{0}.cshtml",
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml"
};
PartialViewLocationFormats = new[] {
"~/Tenant/" + TenantID + "/Views/{1}/{0}.cshtml",
"~/Tenant/" + TenantID + "/Views/Shared/{0}.cshtml"
};
}
}
}
and in my Global.asax
protected void Application_Start()
{
...
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new RazorViewFactory());
}
Everything works except when I load my Tenant sub view Home page, I get the following error.
The view at '~/Tenant/TenantB/Views/Home/Index.cshtml'
must derive from WebViewPage, or WebViewPage<TModel>.
If I load the base home page it works fine with the Razor engine.
Upvotes: 7
Views: 6480
Reputation: 53183
You need to copy the web.config
file from your Views
folder into your Tenant
folder (or make sure it has the same config sections as described here: Razor HtmlHelper Extensions (or other namespaces for views) Not Found)
Upvotes: 21