欢迎来到【一个大西瓜】的博客

不曾为梦想奋斗,拿什么去燃烧青春。有梦之人亦终将老去,但少年心气如昨。
太阳每一个时刻都同时是夕阳和朝阳,每天她沉入西边,意味着她同时从另一面土地升起。
扩大
缩小

在ASPNETCORE中获得所有Action

在ASPNETCORE中获得所有Action

本文旨在记录自己在aspnetcore工作中需要获取所有Action,在查询了资料后进行了几种方法的记录。后期有发现其它方式再进行追加。

一、通过 反射 查看

(该方法过于常见,此处略)

二、通过 ApplicationPartManager 查看

  1. 通过构造函数注入ApplicationPartManager
  2. 通过 PopulateFeature() 方法将数据设置到ControllerFeature实例中;
  3. public class ListController : Controller
    {
        public ListController(ApplicationPartManager applicationPartManager)
        {
            _applicationPartManager = applicationPartManager;
        }
        private ApplicationPartManager _applicationPartManager;
    
        public IEnumerable<dynamic> List()
        {
            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,
                            Attrs = z.CustomAttributes.Select(m => new
                            {
                                FullName = m.AttributeType.FullName,
                            })
                        })
                    }),
                });
            return data;
        }
    }

方法优缺点:

  • 优点:
    使用方便;
    能直接获取所有注册的Controller;
  • 缺点:
    不能获取action信息;
    不能方便的获取路由信息;

三、通过 IActionDescriptorCollectionProvider 查看

  1. 通过构造函数注入IActionDescriptorCollectionProvider 实例;
    private IActionDescriptorCollectionProvider _actionProvider;
    
    public IEnumerable<dynamic> List()
    {
        var actionDescs = _actionProvider.ActionDescriptors.Items.Cast<ControllerActionDescriptor>().Select(x => new 
        {
            ControllerName = x.ControllerName,
            ActionName = x.ActionName,
            DisplayName = x.DisplayName,
            RouteTemplate = x.AttributeRouteInfo.Template,
            Attributes = x.MethodInfo.CustomAttributes.Select(z=>new {
                TypeName = z.AttributeType.FullName,
                ConstructorArgs = z.ConstructorArguments.Select(v => new {
                    ArgumentValue = v.Value
                }),
                NamedArguments = z.NamedArguments.Select(v => new {
                    v.MemberName,
                    TypedValue = v.TypedValue.Value,
                }),
            }),
            ActionId = x.Id,
            x.RouteValues,
            Parameters = x.Parameters.Select(z => new {
                z.Name,
                TypeName = z.ParameterType.Name,
            })
        });
    
        return actionDescs;
    }

方法优缺点

  • 优点:
    能快速查看所有的acton详细信息及其路由信息,包括参数信息,特性等等;
  • 缺点:
    不能方便得查看Controller的信息;

posted on 2019-09-18 18:18  一个大西瓜咚咚咚  阅读(1616)  评论(1编辑  收藏  举报

导航