Asp.net web api delegationhandler中的访问控制器操作方法属性
我有一个自定义属性,并在操作方法中使用它。我需要在委托处理程序中访问此属性信息Asp.net web api delegationhandler中的访问控制器操作方法属性,asp.net-web-api,handler,Asp.net Web Api,Handler,我有一个自定义属性,并在操作方法中使用它。我需要在委托处理程序中访问此属性信息 Controller A { [MyAttribute] public IHttpActionResult MyMethod } public class MyHandler : DelegatingHandler { protected override async Task<HttpResponseMessage> SendAsync( HttpReques
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
}
}
控制器A
{
[我的属性]
公共IHttpActionResult MyMethod
}
公共类MyHandler:DelegatingHandler
{
受保护的覆盖异步任务SendAsync(
HttpRequestMessage请求,CancellationToken CancellationToken)
{
var controllerSelector=GlobalConfiguration.Configuration.Services.GetHttpControllerSelector();
var controllerDescriptor=controllerSelector.SelectController(请求);
//这里我想访问controllerA操作方法MyMethod元数据
//因此,我可以检查actionmethod是否具有自定义属性,并执行我的流程
}
}
在这里,我想访问
controllerA
action-methodMyMethod
元数据,这样我就可以检查actionmethod是否具有自定义属性,并执行我的流程。请给我一些建议。我使用了一种稍微不同的方法
我使用GlobalConfiguration.Configuration.Services.GetApiExplorer().ApiDescriptions并从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();
var-api=GlobalConfiguration.Configuration.Services.GetApiExplorer().apisdescriptions.FirstOrDefault(p=>string.Compare(p.RelativePath,request.RequestUri.LocalPath.Substring(1),StringComparison.OrdinalIgnoreCase)==0);
var MyAttributeInfo=api.ActionDescriptor.GetCustomAttributes().FirstOrDefault();
您需要使用反射。