ASP.Net MVC 3 显示模板和编辑器模板自定义位置,如何? [英] ASP.Net MVC 3 Display-templates and Editor-Template Custom Location, how to?

查看:22
本文介绍了ASP.Net MVC 3 显示模板和编辑器模板自定义位置,如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i am going Nuts, i am using MVCContrib, to create pluggable site using Portable Areas, and everything is working well so far, except that when i started using MVC Templates, what is happening is if i put the The templates in the respective folder of the View it works, examples

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

但我真正想要的是能够创建通用模板集并从主机应用程序或便携式区域使用它,为此我创建了一个名为 DisplayTemplates 的新便携式区域(利用 MVCContrib 编译视图的能力),在这里是可移植的Area结构

but what i want really is the ability to create common templates Set and utilize it from either Host Application or Portable Area, so to do that i created a new Portable Area Called DisplayTemplates(to utilize MVCContrib Ability to compile Views), here is the portable Area structure

DisplayTemplates
|-Views
  |-CommentTemplate.cshtml

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

now in my host Application i have created a Test Model and added UIHint Attribute

public class HostModel
    {


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

但它不起作用,所以我认为它与部分视图位置有关,所以我创建了一个 CustomView 引擎来查找该位置的部分视图并将其注册到 Global.asax,这里有一个简短的想法,所以我不会让你厌烦完整的代码

but it is not working, so i thought it has something to do with Partial Views Location so i created a CustomView Engine to find Partial Views in that Location and registerd it in Global.asax, here is a short idea about so i wont bore you with full code

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 似乎不起作用,这是一个示例

what is even more weird, is that it seems that that UIHint with Explicit location to Display Template, does not work, here is an example

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

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

again sorry for the long post, but i gave up on finding a solution, so any help would be totally appreciated.

推荐答案

Danny 是正确的.模板的查找方式与部分视图的查找方式相同.

Danny is correct. The Templates are found the same way that Partial Views are found.

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

By default the WebFormViewEngine and RazorViewEngine are going to search the following locations for a template.

对于显示模板:

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

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

对于编辑器模板:

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

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

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

I think the name of the sub-directories (i.e., "DisplayTemplates" and "EditorTemplates") are hard-coded into MVC somewhere (I know it's open source and I could find it, but I'm not going to).

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

I think the easiest way to change the location somewhat is to override the ViewEngine. My custom ViewEngine is pretty complicated at this point, but I suspect you could get away with the following.

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

Let's say you want your templates to be in ~/Views/Templates.

创建一个继承自您现在使用的视图引擎(可能是 WebFormViewEngine 或 RazorViewEngine)的类.添加一个空的构造函数.它应该是这样的:

Create a class that inherits from the view engine you're using now (probably WebFormViewEngine or RazorViewEngine). Add an empty constructor. It should looks like this:

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 方法中:

Now, add the following lines to the Application_Start method of Global.asax.cs:

ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new MySiteViewEngine());

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

At this point, compile and run your application. Everything should be running exactly like it is running now. You're basically using the same view engine you were using before.

但是现在,我们想在查找 PartialViews 时添加一个位置来搜索.这只需添加到 PartialViewLocationFormats 即可完成.因此,现在在自定义视图引擎的构造函数中,您希望像这样添加到基类的数组中:

But now, we want to add a location to search when looking for PartialViews. This is simply done by adding to the PartialViewLocationFormats. So, now in the constructor of your custom view engine, you want to add to the base class' array like so:

base.PartialViewLocationFormats = new string[] {
    "~/Views/Templates/{0}.cshtml"
}.Union(base.PartialViewLocationFormats).ToArray<string>();

关于上述的几点说明:

  • 上面的条目将使您的视图引擎在 ~/Views/Templates/DisplayTemplates/String.cshtml 中查找字符串显示模板.
  • 这些视图引擎中的位置格式包括文件扩展名,因此如果您使用 Razor/C# 使用cshtml",Razor/VB 使用vbhtml",WebForms 添加aspx"和ascx".
  • 按照我上面的做法,我将我的位置格式添加到列表顶部,但保留所有默认位置.您可以考虑删除这些内容.
  • 观察当前的格式,您会发现您还将在格式的 {1} 位置获得一个控制器,因此如果您想在每个控制器下都有一个 Templates 目录,您可以这样做.
  • 小心,一旦您开始使用视图引擎移动事物,它就会让人上瘾.您可能会发现自己四处走动.

祝你好运.

这篇关于ASP.Net MVC 3 显示模板和编辑器模板自定义位置,如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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