我有自定义属性,我在我的动作方法中使用它。我需要在委托处理程序中访问此属性信息。
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
}
}
在这里,我想访问controllerA
操作方法MyMethod
元数据,以便检查actionmethod是否具有自定义属性并执行我的流程。请告诉我这个。
答案 0 :(得分:1)
我使用了一点点不同的方法,
我使用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();