0

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

现在当我这样做时:

public class ConfigViewEngine : RazorViewEngine
{
    public ConfigViewEngine()
    {
        var existingViewLocationFormats = new List<string>();
        var partialViewLocationFormats = new List<string>();

        //Folder Structure: Views\Desktop\Home and Views\Mobile\Home
        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";

当我转到 /Home/Dashboard 时出现以下错误

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

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

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

4

1 回答 1

0

弄清楚了。我不应该把那个代码放在 _ViewStart.cshtml 中,我应该把它放在 Global.asax.cs

于 2012-10-08T20:16:40.610 回答