2014-07-23 39 views
2

我有單獨的dll與控制器和視圖(與通常的文件夾結構)。這裏是我的自定義視圖引擎:自定義RazorViewEngine無法正常工作

public class SettingsViewEngine : RazorViewEngine 
{ 
    public SettingsViewEngine() 
    { 
     //if I commit this nothing changes 
     MasterLocationFormats = new[] 
     { 
      "~/bin/Views/{1}/{0}.cshtml", 
      "~/bin/Views/Shared/{0}.cshtml" 
     }; 

     ViewLocationFormats = new[] 
     { 
      "~/bin/Views/{1}/{0}.cshtml", //if I commit this line it starts look at 
              //at shared folder.Looks like only first 
              //line of ViewLocationFormats array used always. 
      "~/bin/Views/Shared/{0}.cshtml" 
     }; 
    } 
} 

- 視圖被複制到bin文件夾。沒有佈局屬性一切正常。但是當它被設置我得到錯誤它不能被發現在

"~/bin/Views/MyLayout.cshtml" 

任何想法?謝謝。

更新

我有更新我的旁邊ovveride搜索引擎:

protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath) 
    { 
     // "~/bin/Views/Shared/_SettingsManagerLayout.cshtml" works (able find) 
     return base.CreateView(controllerContext, viewPath, masterPath); 
    } 

調試時,我能夠看到,主空string.If我通過路徑直接我得到了奇怪的錯誤from layout -

System.Web.WebPages.Html.HtmlHelper'不包含'ActionLink'的定義和最佳擴展方法重載

System.Web.Mvc.Html.LinkExtensions.ActionLink(System.Web.Mvc.HtmlHelper,字符串,字符串對象)」具有一些無效參數

這裏是如何看我的看法:

@inherits System.Web.Mvc.WebViewPage 
@{ 
    Layout="_SettingsManagerLayout.cshtml"; 
} 

... 

更新

的幾點思考:我有錯誤 -

佈局頁「_SettingsManagerLayout.cshtml」無法在以下路徑找到:「〜/斌/瀏覽/設置/ _SettingsManagerLayout.cshtml」

但我希望視圖引擎將檢查指定的幾個路徑在MasterLocationFormats(ViewLocationFormats)中,這是通過具有相同視圖引擎的根項目爲控制器視圖完成的。

結束診斷

自定義視圖引擎忽略來自ViewLocationFormats MasterLocationFormats和僅使用第一項。

+1

你是如何設置的佈局_ViewStart.cshtml文件?像'Layout =「〜/ bin/Views/Shared/_Layout.cshtml」;'? –

+0

沒有。無bin前綴。 – tony

回答

0

你在你的觀點明確

Layout = "~/Views/Shared/_Layout.cshtml"; 

〜由虛擬路徑提供翻譯成你的Web應用程序根設置佈局 - 沒有發動機進入與那部戲。

什麼工作要麼指定這樣的正確的絕對路徑:

Layout = "~/bin/Views/Shared/_Layout.cshtml"; 

或指定給喜歡你的佈局視圖從您的視圖的相對路徑:

Layout = "../Shared/_Layout.cshtml"; 
相關問題