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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With