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

查看:83
本文介绍了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能力编译查看),在这里是便携式区域结构

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中registerd吧,这里是关于这样一个简短的想法我孔不会与你充分code

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

再次为长期职位抱歉,但我放弃了寻找解决方案,所以任何帮助将是完全AP preciated。

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

推荐答案

丹尼是正确的。该模板发现了同样的方式,部分景观被找到。

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.

有关显示模板:

〜/查看/ {}控制器/ DisplayTemplates
  〜/查看/共享/ DisplayTemplates

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

有关编辑模板:

〜/查看/ {}控制器/ EditorTemplates
  〜/查看/共享/ EditorTemplates

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

我觉得子目录的名称(即DisplayTemplates和EditorTemplates)是硬coded到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).

我觉得有点改变位置,最简单的方法是重写视图引擎。我的自定义视图引擎是pretty在这一点上很复杂,但我怀疑你可能逃脱以下。

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.

比方说,你希望你的模板在〜/查看/模板。

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

一个有关上述两点要注意:

A couple of notes about the above:


  • 上面的条目将让您的视图引擎查找在〜/查看/模板/ DisplayTemplates / String.cshtml。
  • 字符串显示模板使它
  • 在这些视图引擎的位置格式包括文件扩展名,因此,如果您正在使用剃刀/ C#用CSHTML剃刀/ VB使用vbhtml,WebForms的增加的aspx和ASCX。

  • 我做的上面,我加入我的位置格式列表的顶部,但保持所有的默认位置的方式。你可能会考虑取消其他。

  • 观看当前格式,你会看到,你也将获得在格式{1}位置的控制器,所以如果你想有每个控制器你可以下一个模板目录。

  • 小心,一旦你开始使用一个视图引擎走动的东西,它就会​​上瘾。你可能会发现自己走动的一切。

  • The entry above will make it so that your view engine looks for the String display template at ~/Views/Templates/DisplayTemplates/String.cshtml.
  • The location format in these view engines includes the file extension, so if you're using Razor/C# use "cshtml", Razor/VB use "vbhtml", WebForms add "aspx" and "ascx".
  • The way I'm doing it above, I'm adding my location format to the top of the list but keeping all the default locations. You might consider removing those.
  • Watch the current formats and you'll see that you will also get a controller in the {1} position in the format, so if you wanted to have a Templates directory underneath every controller you could.
  • Careful, once you get started moving things around with a view engine, it gets addictive. You might find yourself moving everything around.

祝你好运。

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

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