这比 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/。
请注意,MVC 会在转到 /Views/Shared/DisplayTemplates 之前首先检查与您的视图在同一目录中的 /DisplayTemplates 文件夹,因此您也可以将它们移动到使用其相应 HTML 帮助程序的同一视图目录中。
我没试过,但指定模板时也可以使用完整的视图路径(即~/Views/GeneralTemplates/SiteMapPathHelperModel.cshtml)。
@Html.MvcSiteMap().SiteMapPath("~/Views/GeneralTemplates/SiteMapPathHelperModel.cshtml")
重要提示:如果您像这样更改所有模板的位置,您可能需要遍历递归模板并更改其中的所有DisplayFor 位置。 p>
@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 帮助程序来解决此问题。