解读ASP.NET 5 & MVC6系列(16):自定义View视图文件查找逻辑
2015-06-02 09:07
746 查看
之前MVC5和之前的版本中,我们要想对View文件的路径进行控制的话,则必须要对
然后,通过修改MVcOptions的实例属性ViewEngines即可完成对视图引擎的替换,代码如下:
这样,系统在查找视图文件的时候,就会按照新注册的
在上述自定义的
最后,我们在
IViewEngine接口的
FindPartialView或
FindView方法进行重写,所有的视图引擎都继承于该
IViewEngine接口,比如默认的
RazorViewEngine。但新版本MVC6中,对视图文件的路径方式却不太一样了,目前有两种方式,一种是通过
RazorViewEngine,另外一种是通过新特性
IViewLocationExpander接口。
通过RazorViewEngine来控制View路径
在新版的RazorViewEngine中,该类提供了两个虚属性(
AreaViewLocationFormats和
ViewLocationFormats),可以用于重写控制,而不必再对
FindPartialView或
FindView方法进行重写,示例如下:
public class ThemeViewEngine : RazorViewEngine { public ThemeViewEngine(IRazorPageFactory pageFactory, IRazorViewFactory viewFactory, IViewLocationExpanderProvider viewLocationExpanderProvider, IViewLocationCache viewLocationCache) : base(pageFactory, viewFactory, viewLocationExpanderProvider, viewLocationCache) { } public override IEnumerable<string> AreaViewLocationFormats { get { var value = new Random().Next(0, 1); var theme = value == 0 ? "Theme1" : "Theme2"; // 可通过其它条件,设置皮肤的种类 return base.AreaViewLocationFormats.Select(f => f.Replace("/Views/", "/Views/" + theme + "/")); } } public override IEnumerable<string> ViewLocationFormats { get { var value = new Random().Next(0, 1); var theme = value == 0 ? "Theme1" : "Theme2"; // 可通过其它条件,设置皮肤的种类 return base.ViewLocationFormats.Select(f => f.Replace("/Views/", "/Views/" + theme + "/")); } } }
然后,通过修改MVcOptions的实例属性ViewEngines即可完成对视图引擎的替换,代码如下:
services.AddMvc().Configure<MvcOptions>(options => { options.ViewEngines.Clear(); options.ViewEngines.Add(typeof(ThemeViewEngine)); });
这样,系统在查找视图文件的时候,就会按照新注册的
ThemeViewEngine的逻辑来执行。
通过IViewLocationExpander来控制View路径
在MVC6中,微软还提供了另外一种新的方式来控制View文件的路径,那就是IViewLocationExpander接口,通过实现该接口即可实现自定义逻辑,并且也可以使用相关的上下文对象。示例如下:
public class ThemeViewLocationExpander : IViewLocationExpander { public void PopulateValues(ViewLocationExpanderContext context) { var value = new Random().Next(0, 1); var theme = value == 0 ? "Theme1" : "Theme2"; context.Values["theme"] = theme; } public virtual IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations) { return viewLocations.Select(f => f.Replace("/Views/", "/Views/" + context.Values["theme"] + "/")); } }
在上述自定义的
IViewLocationExpander中,实现了2个方法分别是
PopulateValues和
ExpandViewLocations,
PopulateValues方法可以让我们想
ViewLocationExpanderContext上下文中添加响应的键值对以便后续使用,通过,我们可以利用通过该上下文对象,来查找
ActionContext和
HttpContext对象,以便利用这些对象做响应的判断操作;而
ExpandViewLocations方法,只会在没有View缓存或在View缓存里找不到对应key的View文件时才会调用该方法,在该方法内,我们可以动态返回视图的位置。
最后,我们在
Startup.cs里通过修改
RazorViewEngineOptions实例对象的
ViewLocationExpanders属性,来实现注册目的,代码如下:
services.Configure<RazorViewEngineOptions>(options => { options.ViewLocationExpanders.Add(typeof(ThemViewLocationExpander)); });
同步与推荐
本文已同步至目录索引:解读ASP.NET 5 & MVC6系列相关文章推荐
- Asp.net面试题
- 让Mono 4在Raspberry Pi上飞
- C#ASP.NET 通用扩展函数之 LogicSugar 简单好用
- Asp.net防止盗链
- aspose.word 在书签处插入符号
- 学习ASP.NET MVC5框架揭秘笔记-ASP.NET MVC路由(三)
- asp.net mvc url应用
- ASP.NET4.0课后习题答案
- Asp.net MVC3 文本编辑器KindEditor使用及图片上传浏览
- [SCM]ASPx.net开发供应链系统,用来给供应商查询或做一些单据之类的
- IIS7本地ASP(错误 80004005)解决方法
- 捕捉asp.net上传超过文件大小限制的异常
- ASP.NET 上传控件 “Maximum request length exceeded” 异常
- ASP.NET多个按钮同一事件 不同处理
- asp.net 分页类
- 利用JasperReport+iReport进行Web报表开发
- [Asp.net 5] Localization-简单易用的本地化
- [Asp.net 5] Localization-Asp.net运行时多语言
- ASP.NET在IE9,IE10,IE11中Form表单身份验证失效问题解决方法
- 关于Aspose对于Word操作的一些扩展及思考