zoukankan      html  css  js  c++  java
  • 也玩MVC3.“.NET研究”0 Razor自定义视图引擎来修改默认的Views目录结构 狼人:

    刚刚爱上MVC3.0,几个不眠夜的学习越来越有趣。今天随手尝试自定义Mvc3.0的视图引擎,虽然已成功,但是还发现有点小疑问。随手贴出来希望大家指教指教。

    MVC的视图文件目录被固定/Views目录内,区域视图文件也是被固定在/Areas目录下,出于好奇和对目录名的敏感,尝试修改它。通过reflector找到视图引擎的构造接口类VirtualPathProviderViewEngine

    在MVC2.0中,自定义自己的视图引擎,继承它即可,但在3.0中,上海企业网站制作我发现继承它会缺少一个函数。再reflector获得了BuildManagerViewEngine的抽象类,因为RazorViewEng上海企业网站设计与制作ine继承的是该抽象类。

    所以最直接还是在自己的视图引擎中继承它。

    上海闵行企业网站设计与制作>public class myViewEngine : BuildManagerViewEngine
        {
            // Fields
            internal static readonly string ViewStartFileName = "_ViewStart";
            // Methods
            public myViewEngine()
                : this(null)
            {
            }
            public myViewEngine(IViewPageActivator viewPageActivator)
                : base(viewPageActivator)
            {
                base.AreaViewLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/{1}/{0}.vbhtml", "~/Areas/{2}/Views/Shared/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.vbhtml" };
                base.AreaMasterLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/{1}/{0}.vbhtml", "~/Areas/{2}/Views/Shared/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.vbhtml" };
                base.AreaPartialViewLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/{1}/{0}.vbhtml", "~/Areas/{2}/Views/Shared/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.vbhtml" };
                base.ViewLocationFormats = new string[] { "~/T/{1}/{0}.cshtml",  "~/T/Shared/{0}.cshtml" };
               上海徐汇企业网站制作 base.MasterLocationFormats = new string[] { "~/T/{1}/{0}.cshtml",  "~/T/Shared/{0}.cshtml" };
                base.PartialViewLocationFormats = new string[] { "~/T/{1}/{0}.cshtml",  "~/T/Shared/{0}.cshtml" };
                base.FileExtensions = new string[] { "cshtml", "vbhtml" };
            }
            protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
            {
                string layoutPath = null;
                bool runViewStartPages = false;
                IEnumerable<string> fileExtensions = base.FileExtensions;
                return new RazorView(controllerContext, partialPath, layoutPath, runViewStartPages, fileExtensions, base.ViewPageActivator);
            }
            protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
            {
                string layoutPath = masterPath;
                bool runViewStartPages = true;
                IEnumerable<string> fileExtensions = base.FileExtensions;
                return new RazorView(controllerContext, viewPath, layoutPath, runViewStartPages, fileExtensions, base.ViewPageActivator);
            }
        }
    

    上面是原Razor视图引擎的构造类,我只是拷贝下来修改修改我希望的目录结构,比如将原Views目录改成了T,然后在程序初始化的时候,注册加入这个自己的视图引擎类。

    ViewEngines.Engines.Clear();
    ViewEngines.Engines.Add(new myVi上海徐汇企业网站设计与制作ewEngine());

    将项目中Views目录改名T,调试成功。这样,以后的目录结构我们自己可以任意定义。非常方便。

    但是,有人可能会说那6个目录定义属性都是public的,可以直接调出来修改,结果我也这样试过,没成功,无论我直接调用RazorViewEngine封装,还是VirtualPathProviderViewEngine继承后为属性设定值。都没成功,可能我的方法有问题。 如果你知道,希望可以告诉我。

    另外这样自定义目录结构后,在View和Controller之间不能定位了,VS环境还是认为Views目录是视图文件目录,自己定义的T目录没有了“脚手架”...

    声明:此博有部分内容为转载,版权归原作者所有~
  • 相关阅读:
    【学习笔记/题解】树上启发式合并/CF600E Lomsat gelral
    【学习笔记/题解】虚树/[SDOI2011]消耗战
    【题解】 [GZOI2017]小z玩游戏
    【题解】CF1426E Rock, Paper, Scissors
    【题解】CF1426D Non-zero Segments
    【题解】NOIP2018 填数游戏
    【题解】NOIP2018 旅行
    【题解】NOIP2018 赛道修建
    【题解】时间复杂度
    【题解】「MCOI-02」Convex Hull 凸包
  • 原文地址:https://www.cnblogs.com/waw/p/2218063.html
Copyright © 2011-2022 走看看