How to read action method's attributes in ASP.NET Core MVC?

后端 未结 5 1747
日久生厌
日久生厌 2020-12-08 12:43

Based on this article I\'m trying to create an IActionFilter implementation for ASP.NET Core that can process attributes that are marked on the controller and t

5条回答
  •  臣服心动
    2020-12-08 13:29

    You can access the MethodInfo of the action through the ControllerActionDescriptor class:

    public void OnActionExecuting(ActionExecutingContext context)
    {
        if (context.ActionDescriptor is ControllerActionDescriptor controllerActionDescriptor)
        {
            var actionAttributes = controllerActionDescriptor.MethodInfo.GetCustomAttributes(inherit: true);
        }
    }
    

    The MVC 5 ActionDescriptor class used to implement the ICustomAttributeProvider interface which gave access to the attributes. For some reason this was removed in the ASP.NET Core MVC ActionDescriptor class.

提交回复
热议问题