Custom RazorViewEngine gives page error

I am trying to implement my own viewing engine using Razor. The goal is that the view is in a subfolder to use this view.

I have a view engine derived from 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 the Tenant submenu 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.

+7
source share
1 answer

You need to copy the web.config from the Views folder to the Tenant folder (or make sure that it has the same configuration sections as described here: Razor HtmlHelper Extensions (or other namespaces for views) Not found )

+21
source

All Articles