移花接木:借助 IViewLocationExpander 更换 ASP.NET Core View Component 视图路径

摘要:
端午节在将asp.net项目迁移到家中的asp.netcore时遇到了一个问题。用viewcomponent替换Html.RenderAction后,viewcomponent在运行时找不到视图文件。经过在线搜索,我们了解到原来的ASP。NET Core是如此可预测,以至于我们已经预料到了这种情况,并通过IViewLocationExpander提供了相应的扩展功能。对于这里遇到的问题,只需实现IViewLocationExpander接口并在ExpandViewLocations方法中添加新的视图路径。我们被迫在ComponentViewLocationExpander中发现问题,并在打印嵌入日志后立即发现问题。临时铺路不起作用。您只需更改ViewName即可生成新的viewLocations。

端午节在家将一个 asp.net 项目向 asp.net core 迁移时遇到了一个问题,用 view component 取代 Html.RenderAction 之后,运行时 view component 找不到视图文件。

System.InvalidOperationException: The view 'Components/AggSitePostList/PostList' was not found. The following locations were searched:
/Views/AggSite/Components/AggSitePostList/PostList.cshtml
/Views/Shared/Components/AggSitePostList/PostList.cshtml
/Pages/Shared/Components/AggSitePostList/PostList.cshtml
   at Microsoft.AspNetCore.Mvc.ViewEngines.ViewEngineResult.EnsureSuccessful(IEnumerable`1 originalLocations)
   at Microsoft.AspNetCore.Mvc.ViewComponents.ViewViewComponentResult.ExecuteAsync(ViewComponentContext context)
   at Microsoft.AspNetCore.Mvc.ViewComponents.DefaultViewComponentInvoker.InvokeAsync(ViewComponentContext context)

原先用的是 Html.RenderAction ,视图都放在 Controller 对应的视图路径,对于 AggSiteController ,Html.RenderAction 的视图都放在 /Views/AggSite/ 文件夹中,换成 view component 之后,在 AggSiteController 中运行的 view component 却把 /Views/AggSite/ 置之度外,不把这个路径列为视图文件查找范围。由于视图文件比较多,一个一个创建文件夹并移动视图文件比较麻烦,view compoent 这种不够大度的特性让迁移进程受阻。

有没有什么方法可以让将 /Views/AggSite/ 纳入 view component 搜索视图的范围,让其变得更加宽容呢?

网上搜索后得知原来 ASP.NET Core 料事如神,早已料到这种情况,通过 IViewLocationExpander 提供了对应的扩展能力。

对于这里遇到的问题,只需实现 IViewLocationExpander 接口,在 ExpandViewLocations 方法中添加新的视图路径。

public class ComponentViewLocationExpander : IViewLocationExpander
{
    public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
    {
        if (context.ControllerName + "Controller" == nameof(AggSiteController)
            && viewLocations.Any(l=>l.Contains("Components/")))
        {
            var vcLocation =  "/Views/AggSite/{0}" + RazorViewEngine.ViewExtension;
            viewLocations.ToList().Add(vcLocation);
            return viewLocations;
        }

        return viewLocations;
    }

    public void PopulateValues(ViewLocationExpanderContext context) { }
}

然后在 Startup.ConfigureServices 在注册一下

services.Configure<RazorViewEngineOptions>(o =>
{
    o.ViewLocationExpanders.Add(new ComponentViewLocationExpander());
});

原以为这种临时铺路的变通方法可以轻松搞定问题,但实际运行时发现问题依旧,此路不通。

被迫在 ComponentViewLocationExpander 中埋点排查问题,埋点日志打印出来后立马发现了其中的蹊跷。

ViewName: Components/AggSitePostList/PostList
viewLocations: /Views/{1}/{0}.cshtml;/Views/Shared/{0}.cshtml;/Pages/Shared/{0}.cshtml

原来 view component 的路径信息包含在 ViewName 中,并没有包含在 viewLocations 中,难怪之前的临时铺路不管用。

ViewName 中竟然包含视图文件的路径信息,这种偷懒、投机取巧造成的名不符实,很容易误导人。

知道了问题的真正原因后解决起来就不难了。临时铺路行不通,移花接木任我行,直接修改 ViewName 生成新的 viewLocations 即可。

public class ComponentViewLocationExpander : IViewLocationExpander
{
    public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
    {
        if (context.ControllerName + "Controller" == nameof(AggSiteController)
            && context.ViewName.Contains("Components/"))
        {
            var viewName = context.ViewName.Substring(context.ViewName.LastIndexOf("/") + 1);
            return new string[] { "/Views/AggSite/" + viewName + RazorViewEngine.ViewExtension };
        }

        return viewLocations;
    }

    public void PopulateValues(ViewLocationExpanderContext context) { }
}

免责声明:文章转载自《移花接木:借助 IViewLocationExpander 更换 ASP.NET Core View Component 视图路径》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇golang解析json配置文件一文带你了解 HTTP 黑科技下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

ActivityManagerService的启动过程

AMS对象随系统进程启动而构建,随着系统进程退出而消亡,可以说,AMS与系统进程共存亡。 先上一张总的启动时序图: 上图分为三个步骤: 初始化系统进程的运行环境; 初始化AMS对象; AMS对象启动的配套工作。 1.初始化系统进程的运行环境 SystemServer是我们理解Android系统进程的入口,它的初始化是从Native层开始的:Zygot...

SSM 三大框架系列:Spring 5 + Spring MVC 5 + MyBatis 3.5 整合(附源码)

之前整理了一下新版本的 SSM 三大框架,这篇文章是关于它的整合过程和项目源码,版本号分别为:Spring 5.2.2.RELEASE、SpringMVC 5.2.2.RELEASE、MyBatis 3.5.2。 背景介绍 ssm-demo 是我发布到 GitHub 上的第一个开源项目,该项目开发时应该是 2016 年底的时候,之后是选择将这个项目开源到...

彻底搞懂Spring类加载(注解方式)

单例预加载默认 单例懒加载   正确的加载时机   错误的加载时机 多例懒加载仅支持懒加载 spring beanfactory类高级用法   反射方式加载类 需要注意的问题 通过 Spring 注册的类一共只有三种加载方式! 环境:spring-context 4.2.6jdk 8Eclipse 4.7 最简单的配置 <?xml ve...

JNDI简介和简单示例

什么是JNDI? The Java Naming and Directory Interface是访问不同名字和目录服务的统一API接口。 不同的服务使用不同的名字格式。 Java程序需要以相同的格式访问数据库,文件,目录,对象和网络。 JNID有两部分接口:应用程序接口和提供服务的接口。在应用程序中使用API来访问名字或目录服务,在一个新的服务中使用SP...

get****Context各个方法分析

1 getApplicationContext分析 该方法为contextImpl类的方法,返回一个ApplicationContext。方法代码为: public Context getApplicationContext() { return (mPackageInfo != null) ? mPackageInfo.getApplication()...

DropDownList无刷新级联下拉(固定级联),Jquery获取JOSN数据

1.HTML页面 <td align="left">                                <asp:DropDownList ID="ddlOne" runat="server">                                </asp:DropDownList>      ...