这比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