0

カスタムRazorViewエンジンを作成しました

今私がするとき:

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 に答える