ASP.NET MVC:何时应创建自定义View Engine [英] ASP.NET MVC: When should I create custom View Engine

查看:80
本文介绍了ASP.NET MVC:何时应创建自定义View Engine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道View Engine是什么,我更喜欢使用Razor视图引擎,因为它的语法比ASPX引擎简单.内置的视图引擎几乎可以为您执行所有任务,然后在什么情况下我应该创建自己的视图引擎,

I know what View Engine is, I preferred to use Razor view engine just because of its simple syntax over ASPX engine. Inbuilt view engine performs almost all task for you, then in what scenario I should create my own view engine,

我用Google搜索了它,但是得到了有关如何创建它而不是何时以及为什么创建它的答案.

I googled it but getting answers for How to create it and not when and why to create it.

任何人都可以帮助我描述实时情况吗?

Can any one help me to describe the real time scenario?

推荐答案

例如,您可以在自定义视图引擎的帮助下更改Razor搜索的视图文件位置.

For example, you can change the view files locations that Razor searches with the help of custom view engine.

通常,在MVC中,将在以下位置搜索部分视图:

Normally, in MVC these locations are searched for partial views:

 // Part of the RazorViewEngine implementation from the Asp.net MVC source code
 PartialViewLocationFormats = new[]
        {
            "~/Views/{1}/{0}.cshtml",
            "~/Views/{1}/{0}.vbhtml",
            "~/Views/Shared/{0}.cshtml",
            "~/Views/Shared/{0}.vbhtml"
        };

然后将例如LayoutsPartialViews文件夹添加到Shared文件夹,并添加部分视图,例如仅用于布局.并将例如ColorfuleHeader.cshtml添加到该位置.并尝试通过以下方式渲染该视图:

Then add for example LayoutsPartialViews folder to Shared folder and add partial views which for example will be used only for layouts. And add for example ColorfuleHeader.cshtml to that location. And try to render that view via this:

  @Html.Partial("ColorfulHeader");

将引发此类异常:

找不到部分视图'ColorfulHeader'或没有视图引擎 支持搜索的位置.以下位置是 搜索...:

The partial view 'ColorfulHeader' was not found or no view engine supports the searched locations. The following locations were searched...:

因此,我们必须将此位置添加到搜索到的位置.为此,我们必须创建我们的自定义视图引擎:

So we must add this location to the searched locations. And for doing this we must create our custom view engine:

 public class CustomLocationViewEngine : RazorViewEngine
    {
        public CustomLocationViewEngine()
        {
            PartialViewLocationFormats = new[] {
                "~/Views/{1}/{0}.cshtml",
                "~/Views/{1}/{0}.vbhtml",
                "~/Views/Shared/{0}.cshtml",
                "~/Views/Shared/{0}.vbhtml",

                "~/Views/Shared/LayoutsPartialViews/{0}.cshtml",
                "~/Views/Shared/LayoutsPartialViews/{0}.vbhtml",
            };
        }
    }

此外,请记住,动作调用者依次转到每个视图引擎,以查看是否可以找到视图.经过 当我们能够将视图添加到集合中时,它已经包含了标准的Razor视图 引擎.为了避免与该实现竞争,我们调用Clear方法删除其他任何 查看可能已注册的引擎,然后调用Add方法注册我们的自定义 执行.

Also, remember that the action invoker goes to each view engine in turn to see if a view can be found. By the time that we are able to add our view to the collection, it will already contain the standard Razor View Engine. To avoid competing with that implementation, we call the Clear method to remove any other view engines that may have been registered, and then call the Add method to register our custom implementation.

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

这篇关于ASP.NET MVC:何时应创建自定义View Engine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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