获取所有路线的列表
在ASP.NET Core中,有没有办法查看Startup中定义的所有路由的列表? 我们使用IRouteBuilder
的MapRoute
扩展方法来定义路由。
我们正在迁移旧的项目WebAPI项目。 我们可以使用GlobalConfiguration.Configuration.Routes
来获取所有路由。
更具体地说,我们在动作filter中执行此操作。
public class MyFilter : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext actionContext) { base.OnActionExecuting(actionContext); // This no longer works // var allRoutes = GlobalConfiguration.Configuration.Routes; // var allRoutes = ??? } }
要获得所有路由,您需要使用MVC的ApiExplorer部分。 您可以使用属性标记所有操作,也可以使用如下惯例:
public class ApiExplorerVisibilityEnabledConvention : IApplicationModelConvention { public void Apply(ApplicationModel application) { foreach (var controller in application.Controllers) { if (controller.ApiExplorer.IsVisible == null) { controller.ApiExplorer.IsVisible = true; controller.ApiExplorer.GroupName = controller.ControllerName; } } } }
在Startup.cs中,在ConfigureServices(...)
添加新的
public void ConfigureServices(IServiceCollection services) { services.AddMvc( options => { options.Conventions.Add(new ApiExplorerVisibilityEnabledConvention()); options. } }
在ActionFilter
您可以使用构造函数注入来获取ApiExplorer:
public class MyFilter : ActionFilterAttribute { private readonly IApiDescriptionGroupCollectionProvider descriptionProvider; public MyFilter(IApiDescriptionGroupCollectionProvider descriptionProvider) { this.descriptionProvider = descriptionProvider; } public override void OnActionExecuting(ActionExecutingContext actionContext) { base.OnActionExecuting(actionContext); // The convention groups all actions for a controller into a description group var actionGroups = descriptionProvider.ApiDescriptionGroups.Items; // All the actions in the controller are given by var apiDescription = actionGroup.First().Items.First(); // A route template for this action is var routeTemplate = apiDescription.RelativePath } }
ApiDescription
,具有RelativePath
,它是该路由的路由模板:
// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.ModelBinding; namespace Microsoft.AspNetCore.Mvc.ApiExplorer { public class ApiDescription { public string GroupName { get; set; } public string HttpMethod { get; set; } public IList ParameterDescriptions { get; } = new List (); public IDictionary
您可以通过以下方式从HttpActionContext获取HttpRouteCollection:
actionContext.RequestContext.Configuration.Routes
的RequestContext
HttpConfiguration
HttpRouteCollection
– 问题更新后 –
ActionExecutingContext具有一个RouteData属性,它inheritance自ControllerContext,后者公开DataTokens属性(这是一个路由值字典)。 它可能与您习惯使用的集合不同,但它确实提供了对该集合的访问:
actionContext.RouteData.DataTokens
DataTokens
你可以看一下这个很棒的GitHub项目:
https://github.com/kobake/AspNetCore.RouteAnalyzer
该项目的自述文件
=======================
AspNetCore.RouteAnalyzer
查看ASP.NET Core项目的所有路由信息。
挑选截图
ASP.NET Core项目的用法
安装NuGet包
- NuGet画廊| AspNetCore.RouteAnalyzer
PM> Install-Package AspNetCore.RouteAnalyzer
编辑Startup.cs
插入代码services.AddRouteAnalyzer();
并且需要在Startup.cs中using
指令,如下所示。
“`cs使用AspNetCore.RouteAnalyzer; //添加
public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddRouteAnalyzer(); // Add }
案例1:查看浏览器的路径信息
插入代码routes.MapRouteAnalyzer("/routes");
进入Startup.cs如下。
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { .... app.UseMvc(routes => { routes.MapRouteAnalyzer("/routes"); // Add routes.MapRoute( name: "default", template: "{controller}/{action=Index}/{id?}"); }); }
然后,您可以访问http://..../routes
以查看浏览器上的所有路由信息。 (此URL /routes
可由MapRouteAnalyzer()
自定义。)
案例2:在VS输出面板上打印路线
将下面的代码块插入Startup.cs。
public void Configure( IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime applicationLifetime, // Add IRouteAnalyzer routeAnalyzer // Add ) { ... // Add this block applicationLifetime.ApplicationStarted.Register(() => { var infos = routeAnalyzer.GetAllRouteInformations(); Debug.WriteLine("======== ALL ROUTE INFORMATION ========"); foreach (var info in infos) { Debug.WriteLine(info.ToString()); } Debug.WriteLine(""); Debug.WriteLine(""); }); }
然后,您可以在VS输出面板上查看所有路径信息。