2012-10-08 24 views
0

我創建了一個自定義的Razor視圖引擎MVC自定義RazorViewEngine初始化工作不

現在,當我做:

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"; 

我得到以下當我前往/首頁/儀表板時出錯

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

任何人提供正確的文件知道爲什麼它不正確初始化?

回答

0

想通了。我不應該把這些代碼放在_ViewStart.cshtml中,我應該把它放在Global.asax.cs中