ASP.NET MVC扩展自定义视图引擎支持多模板&动态换肤skins机制

2023-06-15,,

ASP.NET mvc的razor视图引擎是一个非常好的.NET MVC框架内置的视图引擎。一般情况我们使用.NET MVC框架为我们提供的这个Razor视图引擎就足够了。但是有时我们想在我们的项目支持多模板&skins机制,比如我们可能会有多套的模板,也就是多个View风格,而我们只需要改一下配置文件就可以轻松的改变页面的风格和模板。实现这个功能有两种方式:

一、使用接口IViewEngine自己完成一个类似Razor视图引擎的功能。

二、继承类RazorViewEngine类,重写它的一些方法达到自定义视图引擎的目的。

显然方法二是最简单的,因此我们选最简单方式实现这个功能。

1、首先,我们定义一个一些基础的辅助类

标示支持Skin特性类:

 using System;
/// <summary>
/// 用于标示支持Skin换肤的特性
/// </summary>
public class SupportSkinAttribute : Attribute
{ }

风格配置结点读取类:

 using System;
using System.Configuration;
using System.Web; public class Utils
{
private static string _skinName; public static string SkinName
{
get
{
if (!string.IsNullOrEmpty(_skinName))
{
return _skinName;
}
//模板风格
_skinName = ConfigurationManager.AppSettings["Skin"];
return _skinName;
}
}
}

Helper类:

 public class CustomViewEngineHelper
{
internal static string[] AddNewLocationFormats(IEnumerable<string> defaultLocationFormats,IEnumerable<string> newLocationFormats)
{
List<string> allItems = new List<string>(newLocationFormats);
foreach (string s in defaultLocationFormats)
{
allItems.Add(s);
} return allItems.ToArray();
} internal static string OverrideMasterPage(string masterName, ControllerContext controllerContext)
{
if (NeedChangeMasterPage(controllerContext))
{
masterName = Utils.SkinName;
} return masterName;
} private static bool NeedChangeMasterPage(ControllerContext context)
{
SupportSkinAttribute attr = Attribute.GetCustomAttribute(context.Controller.GetType(), typeof (SupportSkinAttribute)) as SupportSkinAttribute;
return null != attr;
}
}

2、然后,定义CustomRazorViewEngine类

CustomRazorViewEngine.cs:

 public class CustomRazorViewEngine : RazorViewEngine
{
public CustomRazorViewEngine()
{
string[] mastersLocation = new[]{string.Format("~/skins/{0}/views/{0}.cshtml", Utils.SkinName)};
MasterLocationFormats = CustomViewEngineHelper.AddNewLocationFormats(
new List<string>(MasterLocationFormats),
mastersLocation); string[] viewsLocation = new[]{ string.Format("~/skins/{0}/Views/{{1}}/{{0}}.cshtml",Utils.SkinName)};
//视图文件位置路径的格式
ViewLocationFormats =
PartialViewLocationFormats =
CustomViewEngineHelper.AddNewLocationFormats(new List<string>(ViewLocationFormats), viewsLocation);
} //查找视图文件
public override ViewEngineResult FindView(ControllerContext controllerContext,string viewName,string masterName,bool useCache)
{
masterName = CustomViewEngineHelper.OverrideMasterPage(masterName,controllerContext);
return base.FindView(controllerContext,viewName, masterName,useCache);
}
}

上面代码是最核心的部分,我们在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生效

 ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new CustomRazorViewEngine());

上面第一行是清除默认的视图引擎,接下来把我们自定义的CustomRazorViewEngine注册到MVC框架中使用其生效。

使用CustomRazorViewEngine提供的多模板&skins换肤机制,要在Controller类前面加上特性SupportSkin,如下代码:

 [SupportSkin]
public class HomeController
{
//省略其它代码
}

这样ASP.NET MVC视图引擎就支持多模板&skins换肤机制了,我们只需要增加一个风格,在skins文件夹中创建自己的风格的文件夹,并添加相应的视图。最后,在把Web.config的配置结点名为Skin的值改成,相应的风格名称(即skins文件夹的文件夹名),我们以后想换模板就是分分钟的事。

ASP.NET MVC扩展自定义视图引擎支持多模板&动态换肤skins机制的相关教程结束。

《ASP.NET MVC扩展自定义视图引擎支持多模板&动态换肤skins机制.doc》

下载本文的Word格式文档,以方便收藏与打印。