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

端午节在家将一个 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) { }
}
posted @ 2019-06-11 09:55  dudu  阅读(...)  评论(... 编辑 收藏