解读ASP.NET 5 & MVC6系列(16):自定义View视图文件查找逻辑

以前MVC5和以前的版本中,咱们要想对View文件的路径进行控制的话,则必需要对IViewEngine接口的FindPartialViewFindView方法进行重写,全部的视图引擎都继承于该IViewEngine接口,好比默认的RazorViewEngine。但新版本MVC6中,对视图文件的路径方式却不太同样了,目前有两种方式,一种是经过RazorViewEngine,另一种是经过新特性IViewLocationExpander接口。html

经过RazorViewEngine来控制View路径

在新版的RazorViewEngine中,该类提供了两个虚属性(AreaViewLocationFormatsViewLocationFormats),能够用于重写控制,而没必要再对FindPartialViewFindView方法进行重写,示例以下:缓存

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便可完成对视图引擎的替换,代码以下:iview

services.AddMvc().Configure<MvcOptions>(options =>
{
    options.ViewEngines.Clear();
    options.ViewEngines.Add(typeof(ThemeViewEngine));
});

这样,系统在查找视图文件的时候,就会按照新注册的ThemeViewEngine的逻辑来执行。dom

经过IViewLocationExpander来控制View路径

在MVC6中,微软还提供了另一种新的方式来控制View文件的路径,那就是IViewLocationExpander接口,经过实现该接口便可实现自定义逻辑,而且也可使用相关的上下文对象。示例以下:ide

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个方法分别是PopulateValuesExpandViewLocationsPopulateValues方法可让咱们想ViewLocationExpanderContext上下文中添加响应的键值对以便后续使用,经过,咱们能够利用经过该上下文对象,来查找ActionContextHttpContext对象,以便利用这些对象作响应的判断操做;而ExpandViewLocations方法,只会在没有View缓存或在View缓存里找不到对应key的View文件时才会调用该方法,在该方法内,咱们能够动态返回视图的位置。code

最后,咱们在Startup.cs里经过修改RazorViewEngineOptions实例对象的ViewLocationExpanders属性,来实现注册目的,代码以下:orm

services.Configure<RazorViewEngineOptions>(options =>
{
    options.ViewLocationExpanders.Add(typeof(ThemViewLocationExpander));
});

同步与推荐

本文已同步至目录索引:解读ASP.NET 5 & MVC6系列htm

相关文章
相关标签/搜索