.NetCore获取所有路由

23 篇文章 0 订阅

1、通过 ApplicationPartManager 获取

    public class HomeController : ControllerBase
    {
        private ApplicationPartManager _applicationPartManager;

        public HomeController(ApplicationPartManager applicationPartManager)
        {
            _applicationPartManager = applicationPartManager;
        }
        /// <summary>
        /// 获取路由
        /// </summary>
        /// <returns></returns>
        [HttpGet("route")]
        public IActionResult Get()
        {
            var controllerFeature = new ControllerFeature();
            _applicationPartManager.PopulateFeature(controllerFeature);
            var data = controllerFeature.Controllers.Select(x => new
            {
                Namespace = x.Namespace,
                Controller = x.FullName,
                ModuleName = x.Module.Name,
                Actions = x.DeclaredMethods.Where(m => m.IsPublic && !m.IsDefined(typeof(NonActionAttribute))).Select(y => new
                {
                    Name = y.Name,
                    ParameterCount = y.GetParameters().Length,
                    Parameters = y.GetParameters()
                      .Select(z => new
                      {
                          z.Name,
                          z.ParameterType.FullName,
                          z.Position
                      })
                }),
            });
            return Ok(data);
        }
    }

总结:可以获取所有注册的Controller,不方便获取路由信息

2、通过 IActionDescriptorCollectionProvider 获取

public class HomeController : ControllerBase
    {
        private readonly IActionDescriptorCollectionProvider _actionDescriptorCollectionProvider;

        public HomeController(IActionDescriptorCollectionProvider actionDescriptorCollectionProvider)
        {
            _actionDescriptorCollectionProvider = actionDescriptorCollectionProvider;
        }
        /// <summary>
        /// 获取路由
        /// </summary>
        /// <returns></returns>
        [HttpGet("route")]
        public IActionResult Get()
        {
            var routes = _actionDescriptorCollectionProvider.ActionDescriptors.Items.Select(x => new
            {
                Action = x.RouteValues["Action"],
                Controller = x.RouteValues["Controller"],
                Name = x.AttributeRouteInfo.Name,
                Method = x.ActionConstraints?.OfType<HttpMethodActionConstraint>().FirstOrDefault()?.HttpMethods.First(),
                Template = x.AttributeRouteInfo.Template
            }).ToList();
            return Ok(routes);
        }
    }

总结:可以获取所有注册的Controller和路由,但是缺少summary注释信息

3、通过 SwaggerGenerator 获取

    public class HomeController : Controller
    {
        private readonly SwaggerGenerator _swaggerGenerator;
        public HomeController(SwaggerGenerator swaggerGenerator)
        {
            _swaggerGenerator = swaggerGenerator;
        }

        /// <summary>
        /// 获取路由列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("route")]
        public IActionResult ListRoute()
        {
            var list = _swaggerGenerator.GetSwagger("v1");
            List<object> paths = new List<object>();
            foreach (var item in list.Paths)
            {
                if (item.Value.Operations != null && item.Value.Operations.Count > 0)
                {
                    foreach (var operation in item.Value.Operations)
                    {
                        paths.Add(new
                        {
                            Controller = operation.Value.Tags.FirstOrDefault()?.Name,
                            Path = item.Key,
                            HttpMethod = operation.Key.ToString(),
                            Summary = operation.Value.Summary
                        });
                    }

                }
            }
            return Ok(paths);
        }
    }

参考链接:

c# — 获取ASP.NET Core中所有已注册的路由

在ASPNETCORE中获得所有Action - 一个大西瓜咚咚咚 - 博客园

基于.NetCore3.1搭建项目系列 —— 使用Swagger导出文档 (番外篇) - Net Core教程 - 找一找教程网

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值