Is it possible to get template from different directory?

送分小仙女□ 提交于 2021-02-11 15:15:51

问题


I'm using the same cshtml files in different projects so I would like to be able to share the same directory, the 'GeneralTemplates'. So using the @Html.Partial("GeneralTemplates/_Header") works like a charm. But with @Html.MvcSiteMap().SiteMapPath("GeneralTemplates/_Breadcrumbs") is doesn't work, this needs to be in the 'DisplayTemplates' directory and then this works @Html.MvcSiteMap().SiteMapPath("_Breadcrumbs").

Does anyone has an solution for me to be able to have the file in the 'GeneralTemplates' directory? I was thinking maybe I'm able to get the List of Nodes for the Path but I couldn't find it.


回答1:


This is more of an MVC issue than MvcSiteMapProvider, as MvcSiteMapProvider is using the default templated HTML helper behavior.

It took some searching, but I found a way to override this behavior by adding additional paths to the default MVC view search locations: 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();
}

I don't believe it is possible to remove the /DisplayTemplates folder from the path, as that is a convention (to keep it separate from /EditorTemplates). So, the best you will be able to do is make a folder ~/Views/GeneralTemplates/DisplayTemplates/ using the configuration above.

Note that MVC checks for a /DisplayTemplates folder in the same directory as your view first before going to /Views/Shared/DisplayTemplates, so you could also move them to the same view(s) directory where their corresponding HTML helpers are used.

I haven't tried, but it may also be possible to use a complete view path (i.e. ~/Views/GeneralTemplates/SiteMapPathHelperModel.cshtml) when specifying the template.

@Html.MvcSiteMap().SiteMapPath("~/Views/GeneralTemplates/SiteMapPathHelperModel.cshtml")

Important: If you change the location of all of the templates like this, you will probably need to go through the recursive templates and change all of DisplayFor locations within them as well.

@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>
    }
}

You could instead build custom HTML helpers that are non-templated to work around this issue if the other solutions don't work for you.



来源:https://stackoverflow.com/questions/28436032/is-it-possible-to-get-template-from-different-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!