這比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> > </text>
}
}
您可以而不是構建自定義HTML傭工那些非模板來解決這個問題,如果其他的解決方案不爲你工作。
Thnx爲您的迴應!我試過第一個選項,但沒有成功(我的GeneralTemplates位於'Views/Shared'文件夾中)。並且使用完整的路徑也不起作用。所以我要創建自定義HTML助手。 – 2015-02-11 11:16:41