• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

   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";

 

 

 

 项目结构如下:

 

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

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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ASP.NET2.0AJAX中Webservice调用方法发布时间:2022-07-10
下一篇:
Asp.net设计模式笔记之二:应用程序分离与关注点分离发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap