ASP.NET MVC扩展自定义视图引擎支持多模板动态换肤skins机制
ASP.NET mvc的razor视图引擎是一个非常好的.NET MVC框架内置的视图引擎。一般情况我们使用.NET MVC框架为我们提供的这个Razor视图引擎就足够了。但是有时我们想在我们的项目支持多模板&skins机制,比如我们可能会有多套的模板,也就是多个View风格,而我们只需要改一下配置文件就可以轻松的改变页面的风格和模板。实现这个功能有两种方式:
一、使用接口IViewEngine自己完成一个类似Razor视图引擎的功能。
二、继承类RazorViewEngine类,重写它的一些方法达到自定义视图引擎的目的。
显然方法二是最简单的,因此我们选最简单方式实现这个功能。
1、首先,我们定义一个一些基础的辅助类
标示支持Skin特性类:
1 using System; 2 /// <summary> 3 /// 用于标示支持Skin换肤的特性 4 /// </summary> 5 public class SupportSkinAttribute : Attribute 6 { 7 8 }
风格配置结点读取类:
1 using System; 2 using System.Configuration; 3 using System.Web; 4 5 public class Utils 6 { 7 private static string _skinName; 8 9 public static string SkinName 10 { 11 get 12 { 13 if (!string.IsNullOrEmpty(_skinName)) 14 { 15 return _skinName; 16 } 17 //模板风格 18 _skinName = ConfigurationManager.AppSettings["Skin"]; 19 return _skinName; 20 } 21 } 22 }
Helper类:
1 public class CustomViewEngineHelper 2 { 3 internal static string[] AddNewLocationFormats(IEnumerable<string> defaultLocationFormats,IEnumerable<string> newLocationFormats) 4 { 5 List<string> allItems = new List<string>(newLocationFormats); 6 foreach (string s in defaultLocationFormats) 7 { 8 allItems.Add(s); 9 } 10 11 return allItems.ToArray(); 12 } 13 14 15 internal static string OverrideMasterPage(string masterName, ControllerContext controllerContext) 16 { 17 if (NeedChangeMasterPage(controllerContext)) 18 { 19 masterName = Utils.SkinName; 20 } 21 22 return masterName; 23 } 24 25 private static bool NeedChangeMasterPage(ControllerContext context) 26 { 27 SupportSkinAttribute attr = Attribute.GetCustomAttribute(context.Controller.GetType(), typeof (SupportSkinAttribute)) as SupportSkinAttribute; 28 return null != attr; 29 } 30 }
2、然后,定义CustomRazorViewEngine类
CustomRazorViewEngine.cs:
1 public class CustomRazorViewEngine : RazorViewEngine 2 { 3 public CustomRazorViewEngine() 4 { 5 string[] mastersLocation = new[]{string.Format("~/skins/{0}/views/{0}.cshtml", Utils.SkinName)}; 6 MasterLocationFormats = CustomViewEngineHelper.AddNewLocationFormats( 7 new List<string>(MasterLocationFormats), 8 mastersLocation); 9 10 string[] viewsLocation = new[]{ string.Format("~/skins/{0}/Views/{{1}}/{{0}}.cshtml",Utils.SkinName)}; 11 //视图文件位置路径的格式 12 ViewLocationFormats = 13 PartialViewLocationFormats = 14 CustomViewEngineHelper.AddNewLocationFormats(new List<string>(ViewLocationFormats), viewsLocation); 15 } 16 17 //查找视图文件 18 public override ViewEngineResult FindView(ControllerContext controllerContext,string viewName,string masterName,bool useCache) 19 { 20 masterName = CustomViewEngineHelper.OverrideMasterPage(masterName,controllerContext); 21 return base.FindView(controllerContext,viewName, masterName,useCache); 22 } 23 }
上面代码是最核心的部分,我们在CustomRazorViewEngine类构造函数中就按照我们自定约定规则重写了MasterLocationFormats(~/skins/{0}/views/{0}.cshtml)和ViewLocationFormats(~/skins/{0}/Views/{{1}}/{{0}}.cshtml)属性,最后在FindView方法中重写了master的文件名。
如果风格名为lanhu,将按照以下的规则来创建视图文件:
1、MasterLocationFormats(Layout)路径为:~/skins/lanhu/views/lanhu.cshtml
2、ViewLocationFormats(视图文件)路径为:~/skins/lanhu/Views/{1}/{0}.cshtml,其中{1}和{0}分别表示Controller和Action的名字。
3、最后,注册CustomRazorViewEngine
最后,在Appication_Start中加入下面的代码,使用CustomRazorViewEngine生效
1 ViewEngines.Engines.Clear(); 2 ViewEngines.Engines.Add(new CustomRazorViewEngine());
上面第一行是清除默认的视图引擎,接下来把我们自定义的CustomRazorViewEngine注册到MVC框架中使用其生效。
使用CustomRazorViewEngine提供的多模板&skins换肤机制,要在Controller类前面加上特性SupportSkin,如下代码:
1 [SupportSkin] 2 public class HomeController 3 { 4 //省略其它代码 5 }
这样ASP.NET MVC视图引擎就支持多模板&skins换肤机制了,我们只需要增加一个风格,在skins文件夹中创建自己的风格的文件夹,并添加相应的视图。最后,在把Web.config的配置结点名为Skin的值改成,相应的风格名称(即skins文件夹的文件夹名),我们以后想换模板就是分分钟的事。
转载于:https://www.cnblogs.com/itliuyang/p/6872048.html
ASP.NET MVC扩展自定义视图引擎支持多模板动态换肤skins机制相关推荐
- ASP.NET MVC 3: Razor视图引擎中 @: 和text 语法【转载】
ASP.NET MVC 3: Razor视图引擎中 @: 和<text> 语法[转载] (文章没翻译:建议大家读英文原文,看不懂查着看,顺便提高自己的英语水平!) In today's p ...
- ASP.NET MVC 最好的视图引擎是什么?
在ASP.NET Core MVC应用程序中,视图引擎(view engine)负责处理发送给客户端的内容.MVC框架中默认的视图引擎称为Razor,用来为HTML文件添加注释说明并将这些动态内容插入 ...
- (翻译)为你的MVC应用程序创建自定义视图引擎
Creating your own MVC View Engine For MVC Application 原文链接:http://www.codeproject.com/Articles/29429 ...
- ASP.NET MVC 4 (五) 视图
视图引擎与视图 多数情况下控制器action方法返回ViewResult对象,MVC内建action调用器ControllerActionInvoker负责调用控制器action方法并调用视图引擎处理 ...
- ASP.NET MVC扩展库
很多同学都读过这篇文章吧 ASP.NET MVC中你必须知道的13个扩展点,今天给大家介绍一个ASP.NET MVC的扩展库,主要就是针对这些扩展点进行.这个项目的核心是IOC容器,包括Ninject ...
- 为ASP.NET MVC扩展异步Action功能(下)
本文分为上下两部分,您也可以从<Extend ASP.NET MVC for Asynchronous Action>获得全部内容. 执行Action方法 对于执行同步Action的Syn ...
- ASP.NET MVC 的分部视图
1.什么是分部视图,我们应该什么时候应该用? 作为一个对ASP.NET MVC 模型很熟悉的开发者,他们自然想创建一个内容和代码都可以重用的组件,在web 窗体,我们可以创建一个web用户控件或web ...
- ASP.NET MVC 的多国语系支持
ASP.NET MVC 的多国语系支持 posted on 2014-05-14 11:31 stickout 阅读(...) 评论(...) 编辑 收藏 转载于:https://www.cnblog ...
- Element UI 自定义动态换肤(主题)
需求背景:项目需要支持动态换主题,主题色可以随意选择(即用户想用什么颜色的主题就用什么颜色的主题):而element官网给的 自定义主题 只能是项目里写死,无法实现用户动态切换: 方式一:调接口加载C ...
- asp.net动态换肤
直接上Demo:asp.net动态换肤.zip 转载于:https://www.cnblogs.com/wifi/articles/2484600.html
最新文章
- matlab编程实现基于密度的聚类(DBSCAN)
- 浅析C# new和override的区别
- crc生成多项式怎么算_利用system Verilog生成任意CRC多项式
- 【MySQL经典案例分析】 Waiting for table metadata lock
- 程序员,为什么如此迷茫?
- boost::log::dynamic_type_dispatcher用法的测试程序
- MySql查找几个字段的值一样的记录
- riak php7,Laravel中服务提供者的register和boot分别是干什么
- 前端学习(2361):下拉刷新的学习
- 476B. Dreamoon and WiFi
- 隐藏的iscroll元素显示后不能滚动问题
- 由降低系统之间的接口维护和升级的成本想到的。。。
- Linux内核分析 - 网络[十二]:UDP模块 - 收发
- python 仿真模拟_Python SimPy 仿真系列 (1)
- Visdom:Python可视化神器
- 行哥介绍29个国内外接私活渠道,总有一款适合你
- 不用担心JDK17收费了,Oracle 推出 JDK 8 的升级替代品
- 如何批量将多个Excel文件转换为PDF - 批量Excel转PDF转换器快速教程
- 如何量化考核软件开发人员绩效
- 原装世嘉土星手柄(Sega Saturn)转USB小板,软硬件全开源
热门文章
- Bregman 散度
- 你会如何优化应用程序的性能?
- Java中Arrays类的两个方法:deepEquals和equals
- JavaScript或MyEclipse—如何解决js文件导入到MyEclipse工程后出错?
- SqlServer2005日志清理
- Hyperledger Fabric 网络搭建详解
- IDEA中单元测试使用Scanner控制台无法输入
- Linux虚拟机出现卡死且无法结束进程的解决办法
- (秒杀项目) 4.2 用户登录和注册
- (day 07 - dfs or bfs)剑指 Offer 13. 机器人的运动范围