asp.net-mvc-3 – 自定义RazorViewEngine会出现页面错误

前端之家收集整理的这篇文章主要介绍了asp.net-mvc-3 – 自定义RazorViewEngine会出现页面错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用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)
原文链接:https://www.f2er.com/aspnet/248053.html

猜你在找的asp.Net相关文章