[转载]解读ASP.NET 5 & MVC6系列(16):自定义View视图文件查找逻辑 - 汤姆大叔 - 博客园

来源: [转载]解读ASP.NET 5 & MVC6系列(16):自定义View视图文件查找逻辑 – 汤姆大叔 – 博客园

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

通过RazorViewEngine来控制View路径

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

<pre class="csharp"><code class="hljs cs"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">ThemeViewEngine</span> : <span class="hljs-title">RazorViewEngine</span>
{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">ThemeViewEngine</span><span class="hljs-params">(IRazorPageFactory pageFactory,
        IRazorViewFactory viewFactory,
        IViewLocationExpanderProvider viewLocationExpanderProvider,
        IViewLocationCache viewLocationCache)</span>
        : <span class="hljs-title">base</span><span class="hljs-params">(pageFactory,
                viewFactory,
                viewLocationExpanderProvider,
                viewLocationCache)</span>
    </span>{
    }

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">override</span> IEnumerable&lt;<span class="hljs-keyword">string</span>&gt; AreaViewLocationFormats
    {
        <span class="hljs-keyword">get</span>
        {
            <span class="hljs-keyword">var</span> <span class="hljs-keyword">value</span> = <span class="hljs-keyword">new</span> Random().Next(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>);
            <span class="hljs-keyword">var</span> theme = <span class="hljs-keyword">value</span> == <span class="hljs-number">0</span> ? <span class="hljs-string">"Theme1"</span> : <span class="hljs-string">"Theme2"</span>;  <span class="hljs-comment">// 可通过其它条件,设置皮肤的种类</span>
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">base</span>.AreaViewLocationFormats.Select(f =&gt; f.Replace(<span class="hljs-string">"/Views/"</span>, <span class="hljs-string">"/Views/"</span> + theme + <span class="hljs-string">"/"</span>));
        }
    }

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">override</span> IEnumerable&lt;<span class="hljs-keyword">string</span>&gt; ViewLocationFormats
    {
        <span class="hljs-keyword">get</span>
        {
            <span class="hljs-keyword">var</span> <span class="hljs-keyword">value</span> = <span class="hljs-keyword">new</span> Random().Next(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>);
            <span class="hljs-keyword">var</span> theme = <span class="hljs-keyword">value</span> == <span class="hljs-number">0</span> ? <span class="hljs-string">"Theme1"</span> : <span class="hljs-string">"Theme2"</span>;  <span class="hljs-comment">// 可通过其它条件,设置皮肤的种类</span>
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">base</span>.ViewLocationFormats.Select(f =&gt; f.Replace(<span class="hljs-string">"/Views/"</span>, <span class="hljs-string">"/Views/"</span> + theme + <span class="hljs-string">"/"</span>));
        }
    }
}

然后,通过修改MVcOptions的实例属性ViewEngines即可完成对视图引擎的替换,代码如下:

</code></pre>
<pre class="csharp"><code class="hljs cs">services.AddMvc().Configure&lt;MvcOptions&gt;(options =&gt;
{
    options.ViewEngines.Clear();
    options.ViewEngines.Add(<span class="hljs-keyword">typeof</span>(ThemeViewEngine));
});

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

通过IViewLocationExpander来控制View路径

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

<pre><code class="hljs cs"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">ThemeViewLocationExpander</span> : <span class="hljs-title">IViewLocationExpander</span>
{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">PopulateValues</span><span class="hljs-params">(ViewLocationExpanderContext context)</span>
    </span>{
        <span class="hljs-keyword">var</span> <span class="hljs-keyword">value</span> = <span class="hljs-keyword">new</span> Random().Next(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>);
        <span class="hljs-keyword">var</span> theme = <span class="hljs-keyword">value</span> == <span class="hljs-number">0</span> ? <span class="hljs-string">"Theme1"</span> : <span class="hljs-string">"Theme2"</span>;
        context.Values[<span class="hljs-string">"theme"</span>] = theme;
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">virtual</span> IEnumerable&lt;<span class="hljs-keyword">string</span>&gt; <span class="hljs-title">ExpandViewLocations</span><span class="hljs-params">(ViewLocationExpanderContext context,
                                                            IEnumerable&lt;<span class="hljs-keyword">string</span>&gt; viewLocations)</span>
    </span>{
        <span class="hljs-keyword">return</span> viewLocations.Select(f =&gt; f.Replace(<span class="hljs-string">"/Views/"</span>, <span class="hljs-string">"/Views/"</span> + context.Values[<span class="hljs-string">"theme"</span>] + <span class="hljs-string">"/"</span>));
    }
}

在上述自定义的IViewLocationExpander中,实现了2个方法分别是PopulateValuesExpandViewLocationsPopulateValues方法可以让我们想ViewLocationExpanderContext上下文中添加响应的键值对以便后续使用,通过,我们可以利用通过该上下文对象,来查找ActionContextHttpContext对象,以便利用这些对象做响应的判断操作;而ExpandViewLocations方法,只会在没有View缓存或在View缓存里找不到对应key的View文件时才会调用该方法,在该方法内,我们可以动态返回视图的位置。

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

<pre class="csharp"><code class="hljs cs">services.Configure&lt;RazorViewEngineOptions&gt;(options =&gt;
{
    options.ViewLocationExpanders.Add(<span class="hljs-keyword">typeof</span>(ThemViewLocationExpander));
});

同步与推荐

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

 
 
 
赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏