gpt4 book ai didi

asp.net-mvc - ASP.Net MVC 3 显示模板和编辑器模板自定义位置,如何?

转载 作者:行者123 更新时间:2023-12-04 01:37:13 25 4
gpt4 key购买 nike

我要去坚果,
我正在使用 MVCContrib,使用可移植区域创建可插拔站点,到目前为止一切都运行良好,除了当我开始使用 MVC 模板时,发生的情况是如果我将模板放在 View 的相应文件夹中,它可以工作,例子

HostApplication/Views/Home/DisplayTemplates/FirstName.cshtml
HostApplication/Areas/PortableArea_Blog/Views/Home/DisplayTemplates/Auther.cshtml

但我真正想要的是能够创建通用模板集并从主机应用程序或可移植区域使用它,为此我创建了一个名为 DisplayTemplates 的新可移植区域(利用 MVCContrib 编译 View 的能力),这里是可移植区域结构
DisplayTemplates
|-Views
|-CommentTemplate.cshtml

现在在我的主机应用程序中,我创建了一个测试模型并添加了 UIHint 属性
public class HostModel
{


[UIHint("~/Areas/DisplayTemplates/Comment.cshtml")]
public string Name { get; set; }
}

但它不起作用,所以我认为它与部分 View 位置有关,所以我创建了一个 CustomView 引擎来在该位置查找部分 View 并将其注册在 Global.asax 中,这是一个简短的想法,所以我不会让您感到厌烦带完整代码
public class AreaViewEngine : RazorViewEngine
{
public AreaViewEngine()
{

// {0} = View name
// {1} = Controller name

// View locations
ViewLocationFormats = new[]
{
"~/Areas/DisplayTemplates/{0}.cshtml"

};

PartialViewLocationFormats = ViewLocationFormats;

AreaPartialViewLocationFormats = ViewLocationFormats;
}

protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
{
return new RazorView(controllerContext, partialPath, null, true, new[] { "cshtml" });
}

protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
{
return new RazorView(controllerContext, viewPath, masterPath, true, new[] { "cshtml" });
}




}

更奇怪的是,带有显式位置显示模板的 UIHint 似乎不起作用,这是一个示例
public class HostModel
{
//this works
[UIHint("FirstName")]
//this does not work
[UIHint("~/Views/Home/DisplayTemplates/FirstName.cshtml")]
public string Name { get; set; }
}

是的
FirstName.cshtml is in HostApplication/Views/Home/DisplayTemplates/FirstName.cshtml

再次为长篇文章感到抱歉,但我放弃了寻找解决方案,因此我们将不胜感激。

最佳答案

丹尼是对的。模板的查找方式与部分 View 的查找方式相同。

默认情况下,WebFormViewEngine 和 RazorViewEngine 将在以下位置搜索模板。

对于显示模板:

~/Views/{controller}/DisplayTemplates ~/Views/Shared/DisplayTemplates



对于编辑器模板:

~/Views/{controller}/EditorTemplates ~/Views/Shared/EditorTemplates



我认为子目录的名称(即“DisplayTemplates”和“EditorTemplates”)被硬编码到 MVC 的某个地方(我知道它是开源的,我可以找到它,但我不会)。

我认为改变位置的最简单方法是覆盖 ViewEngine。我的自定义 ViewEngine 在这一点上非常复杂,但我怀疑您可以通过以下方式逃脱。

假设您希望模板位于 ~/Views/Templates 中。

创建一个继承自您现在使用的 View 引擎(可能是 WebFormViewEngine 或 RazorViewEngine)的类。添加一个空的构造函数。它应该是这样的:
namespace MySite
{
public class MySiteViewEngine : RazorViewEngine // <<-- or WebFormViewEngine
{
public MySiteViewEngine()
{
// We'll put some code here in a later step
}
}
}

现在,将以下行添加到 Global.asax.cs 的 Application_Start 方法中:
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new MySiteViewEngine());

此时,编译并运行您的应用程序。一切都应该像现在一样运行。您基本上使用的是您之前使用的相同 View 引擎。

但是现在,我们想在查找 PartialViews 时添加一个位置进行搜索。这只需通过添加到 PartialViewLocationFormats 即可完成。因此,现在在自定义 View 引擎的构造函数中,您希望像这样添加到基类的数组中:
base.PartialViewLocationFormats = new string[] {
"~/Views/Templates/{0}.cshtml"
}.Union(base.PartialViewLocationFormats).ToArray<string>();

关于上述的几点说明:
  • 上面的条目将使您的 View 引擎在 ~/Views/Templates/DisplayTemplates/String.cshtml 中查找字符串显示模板。
  • 这些 View 引擎中的位置格式包括文件扩展名,因此如果您使用 Razor/C# 使用“cshtml”,Razor/VB 使用“vbhtml”,WebForms 添加“aspx”和“ascx”。
  • 我在上面做的方式,我将我的位置格式添加到列表的顶部,但保留所有默认位置。您可以考虑删除这些。
  • 观察当前的格式,您将看到您还将在格式的 {1} 位置获得一个 Controller ,因此如果您想在每个 Controller 下都有一个 Templates 目录,您可以。
  • 小心,一旦你开始使用 View 引擎移动东西,它就会让人上瘾。您可能会发现自己在四处移动一切。

  • 祝你好运。

    关于asp.net-mvc - ASP.Net MVC 3 显示模板和编辑器模板自定义位置,如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5176024/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com