The Razor view engine for ASP. NET MVC is a very good built-in view engine for the. As a general rule, the Razor view engine we have provided for us using the. NET MVC Framework is sufficient. But sometimes we want to support multi-template &skins mechanism in our project, for example we may have multiple sets of templates, that is, multiple view styles, and we just need to change the configuration file to easily change the style and template of the page. There are two ways to implement this feature:
First, use the interface Iviewengine yourself to complete a function similar to the Razor view engine.
Second, inheriting the class Razorviewengine class, overriding some of its methods to achieve the purpose of the custom view engine.
Obviously the method two is the simplest, so we choose the simplest way to implement this function.
1, first, we define a number of basic auxiliary classes
Labeling supports skin feature classes:
1 using SYSTEM;2///<SUMMARY>3///For labeling the features that support skin skinning 4//</summary>5 public class Supportskinattribute:attri BUTE6 {7 8}
Style Configuration node Read class:
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 SkinName10 {11< c6/>get12 { !string. IsNullOrEmpty (_skinname)) { _skinname;16 }17 //template style _skinname = configurationmanager.appsettings["Skin"];19 return _skinname;20 }21 }22}
Helper classes:
1 public class Customviewenginehelper 2 {3 internal static string[] Addnewlocationformats (ienumerable<string> Defaultlocationformats,ienumerable<string> newlocationformats) 4 {5 list<string> allItems = new L Ist<string> (Newlocationformats); 6 foreach (string s in Defaultlocationformats) 7 {8 Allitems.add (s); 9}10 11 return Allitems.toarray ();}13 internal static string Overridemasterpage (String mastername, Controller Context controllercontext) (Needchangemasterpage (ControllerContext)) Name = utils.skinname;20}21 return mastername;23}24 private static bool Needchangemaster Page (ControllerContext context) is {Supportskinattribute attr = Attribute.GetCustomAttribute (context. Controller.gettype (), typeof (Supportskinattribute)) as supportskinattribute;28 return null! = attr;29 }30}
2. Then, define the Customrazorviewengine class
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> (Maste Rlocationformats), 8 masterslocation); 9 string[] Viewslocation = new[]{string. Format ("~/skins/{0}/views/{{1}}/{{0}}.cshtml", Utils.skinname)};11//view file location path formats-Viewlocationformats =13 Partialviewlocationformats =14 customviewenginehelper.addnewlocationformats (New list<string> (viewlocationformats), viewslocation); 15}16 17//Find View file public override Viewengineresult Findview (controll Ercontext controllercontext,string viewname,string mastername,bool usecache) {mastername = CustomViewEngi Nehelper.overridemasterpage (mastername,controllercontext); returN base. Findview (Controllercontext,viewname, Mastername,usecache); 22}23}
The above code is the most central part, and we have rewritten the Masterlocationformats (~/skins/{0}/views/{0} in the Customrazorviewengine class constructor according to our custom Convention rules. cshtml) and Viewlocationformats (~/skins/{0}/views/{{1}}/{{0}}.cshtml) properties, and finally, the file name of master is overridden in the Findview method.
If the style name is Lanhu, the view file is created according to the following rules:
1, masterlocationformats (Layout) path is: ~/skins/lanhu/views/lanhu.cshtml
2, Viewlocationformats (depending on the picture) path is: ~/skins/lanhu/views/{1}/{0}.cshtml, where {1} and {0} respectively represent the controller and the name of the action.
3. Finally, register Customrazorviewengine
Finally, add the following code to the Appication_start and use Customrazorviewengine to take effect
1 ViewEngines.Engines.Clear (); 2 ViewEngines.Engines.Add (New Customrazorviewengine ());
The first line above is to clear the default view engine and then register our custom Customrazorviewengine with the MVC framework to take effect.
Using the multi-template &skins skinning mechanism provided by customrazorviewengine, to precede the Controller class with the feature Supportskin, the following code:
1 [supportskin]2 public class HomeController3 {4 //Omit other code 5}
In this way, the ASP. NET MVC View engine supports multi-template &skins skinning mechanism, we just need to add a style, create your own style folder in the Skins folder, and add the corresponding view. Finally, in the configuration of the Web. config node named skin to change the value of the corresponding style name (that is, the folder name of the Skins folder), we would like to change the template is divided into minutes.
Reprinted: ASP. NET MVC extension Custom View engine supports multi-template & dynamic skin skins mechanism