zoukankan      html  css  js  c++  java
  • ASP.NET MVC 扩展自定义视图引擎支持多模板&动态换肤skins机制

    ASP.NET  mvc的razor视图引擎是一个非常好的.NET  MVC 框架内置的视图引擎。一般情况我们使用.NET MVC框架为我们提供的这个Razor视图引擎就足够了。但是有时我们想在我们的

    项目支持多模板&skins机制,比如我们可能会有多套的模板,也就是多个View风格,而我们只需要改一下配置文件就可以轻松的改变页面的风格和模板。实现这个功能有两种方式:

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

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

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

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

    标示支持Skin特性类:

    using System;

    ///用于标示支持Skin换肤的特性

    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类

    CustomRazorVieEngine.cs:

    public class CustomRazorViewEngine:RazorViewEngine

    {

    public CustomRazorViewEngine()

    {

    string[] mastersLocation=new[]{string.Format("~/skins/{0}/views/{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

    最后,在Application_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文件夹的文件夹名)

  • 相关阅读:
    Delphi 2009 新增单元 Character[1]: ToUpper、ToLower
    复制整个文件夹(当然包括嵌套文件夹)
    Delphi 的匿名多线程
    Delphi 2009 泛型容器单元(Generics.Collections)[4]: TDictionary<T>
    Delphi 2009 泛型容器单元(Generics.Collections)[3]: TStack<T>
    Delphi 2009 泛型容器单元(Generics.Collections)[2]: TQueue<T>
    Delphi XE 10.3.3 RSA 签名(IdSSLOpenSSLHeaders)
    Delphi XE 与 Delphi 7 转换
    webacula安装部署流程
    webacula root登陆密码错误解决方案
  • 原文地址:https://www.cnblogs.com/zzp0320/p/6957494.html
Copyright © 2011-2022 走看看