MVC 自定义 RazorViewEngine 初始化不起作用



我创建了一个自定义的 Razor View 引擎

现在当我这样做时:

public class ConfigViewEngine : RazorViewEngine
{
    public ConfigViewEngine()
    {
        var existingViewLocationFormats = new List<string>();
        var partialViewLocationFormats = new List<string>();
        //Folder Structure: ViewsDesktopHome and ViewsMobileHome
        existingViewLocationFormats.Insert(0,
                                         "~/Views/" + ConfigurationManager.AppSettings["configName"] +
                                         "/Shared/{0}.cshtml");
        partialViewLocationFormats.Insert(0,
                                        "~/Views/" + ConfigurationManager.AppSettings["configName"] +
                                        "/Shared/{0}.cshtml");
        existingViewLocationFormats.Insert(0,
                                         "~/Views/" + ConfigurationManager.AppSettings["configName"] +
                                         "/{1}/{0}.cshtml");
        partialViewLocationFormats.Insert(0,
                                        "~/Views/" + ConfigurationManager.AppSettings["configName"] +
                                        "/{1}/{0}.cshtml");
        ViewLocationFormats = existingViewLocationFormats.ToArray();
        PartialViewLocationFormats = partialViewLocationFormats.ToArray();
    }
}

我的_ViewStart.cshtml看起来像

ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new ConfigViewEngine());
Layout = "~/Views/" + ConfigurationManager.AppSettings["configName"] + "/Shared/_Layout.cshtml";

当我转到/主页/仪表板时出现以下错误

The view 'Dashboard' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/Dashboard.aspx
~/Views/Home/Dashboard.ascx
~/Views/Shared/Dashboard.aspx
~/Views/Shared/Dashboard.ascx
~/Views/Home/Dashboard.cshtml
~/Views/Home/Dashboard.vbhtml
~/Views/Shared/Dashboard.cshtml
~/Views/Shared/Dashboard.vbhtml

如果我在 /Views/Home/ 中放置一个空的 Dashboard.cshtml 文件,它将使用该文件,然后在我刷新时,它将从 /Views/configName/Home/Dashboard 提供正确的文件

有人知道为什么它没有正确初始化吗?

通了。我不应该把这段代码放在_ViewStart.cshtml中,我应该把它放在Global.asax中.cs

相关内容

  • 没有找到相关文章

最新更新