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

后端 未结 5 1750
日久生厌
日久生厌 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:47

    Invoking GetCustomAttributes on a method and/or class is slow(er). You should not invoke GetCustomAttributes every request since .net core 2.2, which @Henk Mollema is suggesting. (There is one exception which I will explain later)

    Instead, on application startup time, the asp.net core framework will invoke GetCustomAttributes on the action method and controller for you and store the result in the EndPoint metadata.

    You can then access this metadata in your asp.net core filters via the EndpointMetadata property of the ActionDescriptor class.

    public class CustomFilter : IActionFilter
    {
        public void OnActionExecuting(ActionExecutingContext context)
        {
            // Get attributes on the executing action method and it's defining controller class
            var attributes = context.ActionDescriptor.EndpointMetadata.OfType();
        }
    
        public void OnActionExecuted(ActionExecutedContext context)
        {
        }
    }
    

    If you do not have access to the ActionDescriptor (for example: because you are operating from a Middleware instead of an filter) from asp.net core 3.0 you can use the GetEndpoint extension method to access it's Metadata. For more info see this github issue.

    public class CustomMiddleware
    {
        private readonly RequestDelegate next;
    
        public CustomMiddleware(RequestDelegate next)
        {
            this.next = next;
        }
    
        public async Task Invoke(HttpContext context)
        {
            // Get the enpoint which is executing (asp.net core 3.0 only)
            var executingEnpoint = context.GetEndpoint();
    
            // Get attributes on the executing action method and it's defining controller class
            var attributes = executingEnpoint.Metadata.OfType();
    
            await next(context);
    
            // Get the enpoint which was executed (asp.net core 2.2 possible after call to await next(context))
            var executingEnpoint2 = context.GetEndpoint();
    
            // Get attributes on the executing action method and it's defining controller class
            var attributes2 = executingEnpoint.Metadata.OfType();
        }
    }
    

    Like stated above, Endpoint Metadata contains the attributes for the action method and its defining controller class. This means that if you would want to explicitly IGNORE the attributes applied on either the controller class or the action method, you have to use GetCustomAttributes. This is almost never the case in asp.net core.

提交回复
热议问题