Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取从授权属性调用的api方法的名称_C#_Asp.net Web Api - Fatal编程技术网

C# 获取从授权属性调用的api方法的名称

C# 获取从授权属性调用的api方法的名称,c#,asp.net-web-api,C#,Asp.net Web Api,在我的自定义授权属性代码中,我想确定调用了哪个WebAPI方法 我很感激我可以通过传入名称来实现这一点(参见示例2),但我宁愿不这样做 // Example1 [CustomAuthAttribute] public MyResponse get(string param1, string param2) { ... } // in the prev example I would like to be able to identify the // method from within

在我的自定义授权属性代码中,我想确定调用了哪个WebAPI方法

我很感激我可以通过传入名称来实现这一点(参见示例2),但我宁愿不这样做

// Example1
[CustomAuthAttribute]
public MyResponse get(string param1, string param2)
{
    ...
}
// in the prev example I would like to be able to identify the
// method from within the CustomAuthAttribute code


// Example2
[CustomAuthAttribute(MethodName = "mycontroller/get")]
public MyResponse get(string param1, string param2)
{
    ...
}
// in this example I pass the controller/method names to the
// CustomAuthAttribute code

有什么方法可以让我找到它吗?

如果从
authorized属性派生,您可以通过
HttpActionContext
访问
ActionDescriptor

public class CustomAuthAttribute : AuthorizeAttribute {    
    public override void OnAuthorization(HttpActionContext actionContext) {
        var actionDescriptor = actionContext.ActionDescriptor;
        var actionName = actionDescriptor.ActionName;
        var controllerName = actionDescriptor.ControllerDescriptor.ControllerName;
        //MethodName = "mycontroller/get"
        var methodName = string.Format("{0}/{1}", controllerName, actionName);
    }
}

显示属性的定义我还没有定义-只是计划而已。