自定义RazorViewEngine会出现页面错误

时间:2011-03-14 20:55:18

标签: asp.net-mvc-3 razor

我正在尝试使用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引擎。

1 个答案:

答案 0 :(得分:21)

您需要将web.config文件夹中的Views文件复制到Tenant文件夹中(或确保其具有与此处所述相同的配置部分:Razor HtmlHelper Extensions (or other namespaces for views) Not Found