MVC学习系列——RazorViewEngine扩展

       有时候,我们的项目涉及到多种风格,我们可以通过扩展RazorViewEngine,这样就可以保持后台代码不发生变化。

新建类ThemeViewEngine继承于RazorViewEngine

 1  public class ThemeViewEngine : RazorViewEngine
 2     {
 3         public ThemeViewEngine(string theme)
 4         {
 5             ViewLocationFormats = new[]
 6            {
 7                 "~/Views/Themes/" + theme + "/{1}/{0}.cshtml",
 8                 "~/Views/Themes/" + theme + "/Shared/{0}.cshtml"
 9             };
10 
11             PartialViewLocationFormats = new[]
12             {
13                 "~/Views/Themes/" + theme + "/{1}/{0}.cshtml",
14                 "~/Views/Themes/" + theme + "/Shared/{0}.cshtml"
15             };
16 
17             AreaViewLocationFormats = new[]
18             {
19                 "~Areas/{2}/Views/Themes/" + theme + "/{1}/{0}.cshtml",
20                 "~Areas/{2}/Views/Themes/" + theme + "/Shared/{0}.cshtml"
21             };
22 
23             AreaPartialViewLocationFormats = new[]
24             {
25                 "~Areas/{2}/Views/Themes/" + theme + "/{1}/{0}.cshtml",
26                 "~Areas/{2}/Views/Themes/" + theme + "/Shared/{0}.cshtml"
27             };
28         }
29     }

Global类中,注册这种引擎

1   //不同风格
2             if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["Theme"]))
3             {
4                 var activeTheme = ConfigurationManager.AppSettings["Theme"];
5                 ViewEngines.Engines.Insert(0, new ThemeViewEngine(activeTheme));
6             }

web.config配置具体风格:

1 <add key="Theme" value="China" />

项目文件结构:

View:

   America:

1 @{
2     ViewBag.Title = "ShowTheme";
3 }
4 
5 <h2>ShowTheme   America</h2>

  China:

1 @{
2     ViewBag.Title = "ShowTheme";
3 }
4 
5 <h2>ShowTheme   China</h2>

 

 

运行:

1  public ActionResult ShowTheme()
2         {
3             return View();
4         }

效果图:

 

转载于:https://www.cnblogs.com/xuliang1992/p/5329641.html

asp.net mvc 自定义视图引擎 public ThemeableRazorViewEngine() { AreaViewLocationFormats = new[] { //themes "~/Areas/{2}/Themes/{3}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Themes/{3}/Views/Shared/{0}.cshtml", //default "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.cshtml", }; AreaMasterLocationFormats = new[] { //themes "~/Areas/{2}/Themes/{3}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Themes/{3}/Views/Shared/{0}.cshtml", //default "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.cshtml", }; AreaPartialViewLocationFormats = new[] { //themes "~/Areas/{2}/Themes/{3}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Themes/{3}/Views/Shared/{0}.cshtml", //default "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.cshtml" }; ViewLocationFormats = new[] { //themes "~/Themes/{2}/Views/{1}/{0}.cshtml", "~/Themes/{2}/Views/Shared/{0}.cshtml", //default "~/Views/{1}/{0}.cshtml", "~/Views/Shared/{0}.cshtml", //Admin //"~/Administration/Views/{1}/{0}.cshtml", //"~/Administration/Views/Shared/{0}.cshtml", }; MasterLocationFormats = new[] { //themes "~/Themes/{2}/Views/{1}/{0}.cshtml", "~/Themes/{2}/Views/Shared/{0}.cshtml", //default "~/Views/{1}/{0}.cshtml", "~/Views/Shared/{0}.cshtml" }; PartialViewLocationFormats = new[] { //themes "~/Themes/{2}/Views/{1}/{0}.cshtml", "~/Themes/{2}/Views/Shared/{0}.cshtml", //default "~/Views/{1}/{0}.cshtml", "~/Views/Shared/{0}.cshtml", //Admin //"~/Administration/Views/{1}/{0}.cshtml", //"~/Administration/Views/Shared/{0}.cshtml", }; FileExtensions = new[] { "cshtml" }; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值