端午节在家将一个 asp.net 项目向 asp.net core 迁移时遇到了一个问题,用 view component 取代 Html.RenderAction 以后,运行时 view component 找不到视图文件。html
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 这种不够大度的特性让迁移进程受阻。asp.net
有没有什么方法能够让将 /Views/AggSite/ 归入 view component 搜索视图的范围,让其变得更加宽容呢?spa
网上搜索后得知原来 ASP.NET Core 料事如神,早已料到这种状况,经过 IViewLocationExpander 提供了对应的扩展能力。.net
对于这里遇到的问题,只需实现 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 在注册一下code
services.Configure<RazorViewEngineOptions>(o => { o.ViewLocationExpanders.Add(new ComponentViewLocationExpander()); });
原觉得这种临时铺路的变通方法能够轻松搞定问题,但实际运行时发现问题依旧,此路不通。component
被迫在 ComponentViewLocationExpander 中埋点排查问题,埋点日志打印出来后立马发现了其中的蹊跷。htm
ViewName: Components/AggSitePostList/PostList viewLocations: /Views/{1}/{0}.cshtml;/Views/Shared/{0}.cshtml;/Pages/Shared/{0}.cshtml
原来 view component 的路径信息包含在 ViewName 中,并无包含在 viewLocations 中,难怪以前的临时铺路无论用。blog
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) { } }