解读ASP.NET 5 & MVC6系列(16):自定义View视图文件查找逻辑
2015-06-02 09:07 汤姆大叔 阅读(12430) 评论(6) 编辑 收藏 举报之前MVC5和之前的版本中,我们要想对View文件的路径进行控制的话,则必须要对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系列
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库