我正在尝试使用Razor实现自定义视图引擎.目标是如果视图位于子文件夹中以改为使用该视图.
我的视图引擎来自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" }; } }
}
在我的Global.asax中
protected void Application_Start() { ... ViewEngines.Engines.Clear(); ViewEngines.Engines.Add(new RazorViewFactory()); }
一切正常,除非我加载我的租户子视图主页,我收到以下错误.
The view at '~/Tenant/TenantB/Views/Home/Index.cshtml' must derive from WebViewPage,or WebViewPage<TModel>.
如果我加载基本主页,它可以与Razor引擎一起使用.
解决方法
您需要将您的Views文件夹中的web.config文件复制到Tenant文件夹中(或确保它具有与此处所述相同的配置部分:
Razor HtmlHelper Extensions (or other namespaces for views) Not Found)