Asp.net MVC 自定义ViewEngine的简单实现

   Asp.net MVC 网站换肤需要使用不同模板,可以通过自定义视图引擎来实现这个功能。这里我偷懒,直接继承 VirtualPathProviderViewEngine来实现。

  核心代码如下:

  ThemeViewEngine.cs

   

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;



namespace MvcSkinDemo.Code

{

    public class ThemeViewEngine : VirtualPathProviderViewEngine

    {

        private string _theme;

        /// <summary>

        /// 使用的主题名称

        /// </summary>

        public string Theme

        {

            get { return _theme; }

            set

            {

                _theme = string.IsNullOrEmpty(value) ? "Default" : value;

                SetLocationFormats(_theme);

            }

        }



        public ThemeViewEngine(string themeName)

        {

            Theme = themeName;

        }



        /// <summary>

        /// 设置视图的搜索路径集合

        /// </summary>

        /// <param name="themeName">主题名</param>

        private void SetLocationFormats(string themeName)

        {

            base.MasterLocationFormats = new[] {

                "~/Views/Themes/" + themeName + "/{1}/{0}.master",

                "~/Views/Themes/" + themeName + "/Shared/{0}.master",

                "~/Views/Themes/Shared/{0}.master"

            };



            base.ViewLocationFormats = new[] {

                "~/Views/Themes/" + themeName + "/{1}/{0}.aspx",

                "~/Views/Themes/" + themeName + "/{1}/{0}.ascx",

                "~/Views/Themes/" + themeName + "/Shared/{0}.aspx",

                "~/Views/Themes/" + themeName + "/Shared/{0}.ascx",

                "~/Views/Themes/Shared/{1}/{0}.aspx",

                "~/Views/Themes/Shared/{1}/{0}.ascx",

                "~/Views/Themes/Shared/{0}.aspx",

                "~/Views/Themes/Shared/{0}.ascx"

            };



            base.PartialViewLocationFormats = base.ViewLocationFormats;

        }



        protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)

        {

            return new WebFormView(partialPath);

        }



        protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)

        {

            return new WebFormView(viewPath, masterPath);

        }

    }

}

   Global.asax 设置如下:

 

   protected void Application_Start()

        {

            AreaRegistration.RegisterAllAreas();



            ///初始化ViewEngine

            InitViewEngine();



            RegisterRoutes(RouteTable.Routes);

        }



        private void InitViewEngine()

        {

            

            ViewEngines.Engines.Clear();

            //在RC中目前还没找到好的在Controller中修改ViewEngine的办法

            //只好在这里将顺序换一下,对性能有一点影响

            //ViewEngines.Engines.Add(new ThemeViewEngine(theme));

           

            ViewEngines.Engines.Add(new ThemeViewEngine("default"));

        }

切换视图引擎:



  (ViewEngines.Engines[0] as ThemeViewEngine).Theme = "green";

 

 

 

 项目结构如下:

Asp.net MVC 自定义ViewEngine的简单实现

 

  如果你想定义强大的视图引擎,可以参考这个:

  http://www.cnblogs.com/webabcd/archive/2009/05/14/1456453.html

你可能感兴趣的:(asp.net)