|
剛剛愛(ài)上MVC3.0,幾個(gè)不眠夜的學(xué)習(xí)越來(lái)越有趣。今天隨手嘗試自定義Mvc3.0的視圖引擎,雖然已成功,但是還發(fā)現(xiàn)有點(diǎn)小疑問(wèn)。隨手貼出來(lái)希望大家指教指教。
MVC的視圖文件目錄被固定/Views目錄內(nèi),區(qū)域視圖文件也是被固定在/Areas目錄下,出于好奇和對(duì)目錄名的敏感,嘗試修改它。通過(guò)reflector找到視圖引擎的構(gòu)造接口類VirtualPathProviderViewEngine
在MVC2.0中,自定義自己的視圖引擎,繼承它即可,但在3.0中,我發(fā)現(xiàn)繼承它會(huì)缺少一個(gè)函數(shù)。再reflector獲得了BuildManagerViewEngine的抽象類,因?yàn)?a class="bold" href="http://www.aisto.com/roeder/dotNET/Default.ASPx?Target=code://System.Web.Mvc:3.0.0.0:31bf3856ad364e35/System.Web.Mvc.RazorViewEngine">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技術(shù):也玩MVC3.0 Razor自定義視圖引擎來(lái)修改默認(rèn)的Views目錄結(jié)構(gòu),轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。