是否可以从其他目录获取模板? [英] Is it possible to get template from different directory?

查看:221
本文介绍了是否可以从其他目录获取模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在不同的项目中使用了相同的cshtml文件,因此我希望能够共享相同的目录"GeneralTemplates".因此,使用 @ Html.Partial("GeneralTemplates/_Header")就像一种魅力.但是对于 @ Html.MvcSiteMap().SiteMapPath("GeneralTemplates/_Breadcrumbs")不起作用,它必须位于'DisplayTemplates'目录中,然后才能在 @Html下工作.MvcSiteMap().SiteMapPath("_ Breadcrumbs").

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").

有人对我有解决方案,可以将文件保存在"GeneralTemplates"目录中吗?我在想也许我可以获取路径的节点列表,但找不到.

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.

推荐答案

与MvcSiteMapProvider相比,这更是一个MVC问题,因为MvcSiteMapProvider使用的是默认的模板HTML帮助器行为.

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

进行了一些搜索,但是我找到了一种方法,可以通过向默认的MVC视图搜索位置添加其他路径来覆盖此行为:我可以添加到ASP.NET MVC 3中的Display/EditorTemplates搜索路径?

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();
}

我不认为可以从路径中删除/DisplayTemplates 文件夹,因为这是一个约定(将其与/EditorTemplates 分开).因此,最好的方法是使用上述配置创建一个〜/Views/GeneralTemplates/DisplayTemplates/文件夹.

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.

请注意,在转到/Views/Shared/DisplayTemplates 之前,MVC首先在与您的视图相同的目录中检查/DisplayTemplates 文件夹,因此您也可以移动它们到使用相应HTML帮助器的相同视图目录.

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.

我还没有尝试过,但是在指定模板时也可以使用完整的视图路径(即〜/Views/GeneralTemplates/SiteMapPathHelperModel.cshtml ).

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")

重要提示:如果您要更改所有模板的位置,则可能需要遍历递归模板并更改其中的所有 DisplayFor 位置也是

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

可以构建其他示例无法解决的自定义HTML帮助器,以解决其他问题.

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.

这篇关于是否可以从其他目录获取模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆