2015-02-10 42 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

相关问题