也玩MVC3.0 Razor自定義視圖引擎來修改默認的Views目錄結構

作者: Richwong  來源: 博客園  發布時間: 2011-04-19 10:39  閱讀: 9595 次  推薦: 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
 
 
 

文章列表

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()