您的位置:知识库 » .NET技术

也玩MVC3.0 Razor自定义视图引擎来修改默认的Views目录结构

作者: Richwong  来源: 博客园  发布时间: 2011-04-19 10:39  阅读: 9795 次  推荐: 1   原文链接   [收藏]  

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

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

在MVC2.0中,自定义自己的视图引擎,继承它即可,但在3.0中,我发现继承它会缺少一个函数。再reflector获得了BuildManagerViewEngine的抽象类,因为RazorViewEngine继承的是该抽象类。

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

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

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

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

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

1
1

.NET技术热门文章

    .NET技术最新文章

      最新新闻

        热门新闻