C# 是否可以从不同的目录获取模板?
我在不同的项目中使用相同的cshtml文件,因此我希望能够共享相同的目录“GeneralTemplates”。因此,使用C# 是否可以从不同的目录获取模板?,c#,asp.net-mvc,mvcsitemapprovider,C#,Asp.net Mvc,Mvcsitemapprovider,我在不同的项目中使用相同的cshtml文件,因此我希望能够共享相同的目录“GeneralTemplates”。因此,使用@Html.Partial(“GeneralTemplates/_头”)就像一个符咒。但是对于@Html.MvcSiteMap().SiteMapPath(“GeneralTemplates/\u Breadcrumbs”)是不起作用的,这需要放在“DisplayTemplates”目录中,然后才能起作用@Html.MvcSiteMap().SiteMapPath(\u Bre
@Html.Partial(“GeneralTemplates/_头”)
就像一个符咒。但是对于@Html.MvcSiteMap().SiteMapPath(“GeneralTemplates/\u Breadcrumbs”)
是不起作用的,这需要放在“DisplayTemplates”目录中,然后才能起作用@Html.MvcSiteMap().SiteMapPath(\u Breadcrumbs”)
有没有人能为我提供一个解决方案,让我能够将文件放在“GeneralTemplates”目录中?我在想,也许我能够获得路径的节点列表,但我找不到它。这比MvcSiteMapProvider更像是MVC的问题,因为MvcSiteMapProvider使用默认的模板化HTML帮助器行为 这需要一些搜索,但我找到了一种方法,通过向默认MVC视图搜索位置添加其他路径来覆盖此行为: 我认为无法从路径中删除
/DisplayTemplates
文件夹,因为这是一种约定(将其与/EditorTemplates
分开)。因此,您最好使用上述配置创建一个文件夹~/Views/GeneralTemplates/DisplayTemplates/
请注意,在转到/Views/Shared/DisplayTemplates
之前,MVC首先检查视图所在目录中的/DisplayTemplates
文件夹,因此您也可以将它们移动到使用相应HTML帮助程序的相同视图目录中
我没有尝试过,但在指定模板时也可以使用完整的视图路径(即~/Views/GeneralTemplates/sitemappath helpermodel.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>
}
}
@model mvcsitemprovider.Web.Html.Models.SiteMapPathHelperModel
@使用System.Web.Mvc.Html
@使用System.Linq
@使用MvcSiteMapProvider.Web.Html.Models
@foreach(模型中的var节点){
@Html.DisplayFor(m=>node);@*//Thnx用于您的响应!我尝试了第一个选项,但没有成功(我的GeneralTemplates位于Views/Shared
文件夹中)。并且使用完整路径也没有成功。因此,我将创建自定义Html帮助程序。
@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>
}
}