|
刚刚爱上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); } }
NET技术:也玩MVC3.0 Razor自定义视图引擎来修改默认的Views目录结构,转载需保留来源!
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。