In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
本篇文章为大家展示了如何自定义View视图文件查找逻辑,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
之前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 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 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(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 ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable 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(options =>{ options.ViewLocationExpanders.Add(typeof(ThemViewLocationExpander));});上述内容就是如何自定义View视图文件查找逻辑,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.