Access Controller action methods attributes in delegationhandler

867 views Asked by At

I have a custom attribute and I use it in my action methods. I need to access this attribute information in delegation handler.

Controller A
{
   [MyAttribute]
   public IHttpActionResult MyMethod
}

public class MyHandler : DelegatingHandler
{
   protected override async Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request, CancellationToken cancellationToken)
   {
     var controllerSelector = GlobalConfiguration.Configuration.Services.GetHttpControllerSelector();
     var controllerDescriptor = controllerSelector.SelectController(request);
     //Here I want to access controllerA action method MyMethod metadata 
     //so I can check if actionmethod has custom attribute or not and do my process
   }
}

Here I want to access controllerA action method MyMethod metadata so I can check if actionmethod has custom attribute or not and do my process. Please advise me on this.

1

There are 1 answers

0
Bilgehan On

I use a little bit different approach,

I use GlobalConfiguration.Configuration.Services.GetApiExplorer().ApiDescriptions and match it from request.RequestUri

var api = GlobalConfiguration.Configuration.Services.GetApiExplorer().ApiDescriptions.FirstOrDefault(p => string.Compare(p.RelativePath , request.RequestUri.LocalPath.Substring(1), StringComparison.OrdinalIgnoreCase) == 0);
         
var MyAttributeInfo =  api.ActionDescriptor.GetCustomAttributes<MyAttribute>().FirstOrDefault();