2015-02-10 96 views
0

我在不同的項目中使用相同的cshtml文件,所以我想能夠共享相同的目錄,'GeneralTemplates'。所以使用@Html.Partial("GeneralTemplates/_Header")就像是一種魅力。但與@Html.MvcSiteMap().SiteMapPath("GeneralTemplates/_Breadcrumbs")是行不通的,這需要在'DisplayTemplates'目錄,然後這工作@Html.MvcSiteMap().SiteMapPath("_Breadcrumbs")是否有可能從不同的目錄中獲取模板?

有沒有人有解決方案,我可以在'GeneralTemplates'目錄中的文件?我在想,也許我能夠得到Path的節點列表,但我找不到它。

回答

0

這比MvcSiteMapProvider更像MVC問題,因爲MvcSiteMapProvider正在使用默認的模板化HTML幫助器行爲。

它採取了一些搜索,但我發現了一種通過增加額外的路徑到默認的MVC視圖搜索位置重寫此行爲: Can I Add to the Display/EditorTemplates Search Paths in ASP.NET MVC 3?

System.Web.Mvc.RazorViewEngine rve = (RazorViewEngine)ViewEngines.Engines 
    .Where(e=>e.GetType()==typeof(RazorViewEngine)) 
    .FirstOrDefault(); 

string[] additionalPartialViewLocations = new[] { 
    "~/Views/GeneralTemplates/{0}.cshtml" 
}; 

if(rve!=null) 
{ 
    rve.PartialViewLocationFormats = rve.PartialViewLocationFormats 
    .Union(additionalPartialViewLocations) 
    .ToArray(); 
} 

我不相信這是可能去除/DisplayTemplates文件夾的路徑,因爲這是一個約定(以保持它與/EditorTemplates分開)。所以,你可以做的最好的就是使用上面的配置創建一個文件夾~/Views/GeneralTemplates/DisplayTemplates/

請注意,在轉到/Views/Shared/DisplayTemplates之前,MVC首先在您的視圖的同一目錄中檢查/DisplayTemplates文件夾,以便您還可以將它們移動到使用相應HTML幫助程序的相同視圖目錄中。

我還沒有嘗試過,但在指定模板時也可以使用完整的視圖路徑(即~/Views/GeneralTemplates/SiteMapPathHelperModel.cshtml)。

@Html.MvcSiteMap().SiteMapPath("~/Views/GeneralTemplates/SiteMapPathHelperModel.cshtml") 

重要:如果你改變這一切的像這樣的模板的位置,你可能需要去通過遞歸模板和更改所有的DisplayFor位置內他們。

@model MvcSiteMapProvider.Web.Html.Models.SiteMapPathHelperModel 
@using System.Web.Mvc.Html 
@using System.Linq 
@using MvcSiteMapProvider.Web.Html.Models 

@foreach (var node in Model) { 
    @Html.DisplayFor(m => node); @* // <-- Need to add the diplaytemplate here, too *@ 

    if (node != Model.Last()) { 
     <text> &gt; </text> 
    } 
} 

可以而不是構建自定義HTML傭工那些非模板來解決這個問題,如果其他的解決方案不爲你工作。

+0

Thnx爲您的迴應!我試過第一個選項,但沒有成功(我的GeneralTemplates位於'Views/Shared'文件夾中)。並且使用完整的路徑也不起作用。所以我要創建自定義HTML助手。 – 2015-02-11 11:16:41

相關問題