星空网 > 软件开发 > ASP.net

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" />

项目文件结构:
MVC学习系列——RazorViewEngine扩展

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     }

效果图:
MVC学习系列——RazorViewEngine扩展

 




原标题:MVC学习系列——RazorViewEngine扩展

关键词:ie

ie
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

中东节日大全:https://www.goluckyvip.com/tag/49062.html
中东跨境电商汇:https://www.goluckyvip.com/tag/49063.html
中东快递:https://www.goluckyvip.com/tag/49066.html
中东快递专线:https://www.goluckyvip.com/tag/49067.html
中东商璐:https://www.goluckyvip.com/tag/49069.html
LED灯具:https://www.goluckyvip.com/tag/4907.html
在古巴做游轮 古巴旅游项目:https://www.vstour.cn/a/363194.html
西藏旅游攻略自驾游需要多少天 去西藏旅游自驾游要多久时间:https://www.vstour.cn/a/363195.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流