Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.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#MVC中检查方法是否具有AllowAnonymous属性_C#_Asp.net Mvc - Fatal编程技术网

如何在C#MVC中检查方法是否具有AllowAnonymous属性

如何在C#MVC中检查方法是否具有AllowAnonymous属性,c#,asp.net-mvc,C#,Asp.net Mvc,我有一个控制器,其中一些方法具有authorize属性,一些方法具有AllowAnonymous属性 所以,我想在调用方法时检查是Authorize还是AllowAnonymous string actionName = ViewContext.RouteData.Values["Action"] MethodInfo method = type.GetMethod(actionName); var attribute = method.GetCustomAttributes(typeof(Di

我有一个控制器,其中一些方法具有authorize属性,一些方法具有AllowAnonymous属性 所以,我想在调用方法时检查是Authorize还是AllowAnonymous

string actionName = ViewContext.RouteData.Values["Action"]
MethodInfo method = type.GetMethod(actionName);
var attribute = method.GetCustomAttributes(typeof(DisplayNameAttribute), false);
if (attribute.Length > 0)
   actionName = ((DisplayNameAttribute)attribute[0]).DisplayName;
else 
   actionName = type.Name;
这是我的控制器,方法名为login

[AllowAnonymouse]

Public ActionResult Login(){}
所以在不同的类中,我想检查这个方法是Authorize还是AllowAnonymous

string actionName = ViewContext.RouteData.Values["Action"]
MethodInfo method = type.GetMethod(actionName);
var attribute = method.GetCustomAttributes(typeof(DisplayNameAttribute), false);
if (attribute.Length > 0)
   actionName = ((DisplayNameAttribute)attribute[0]).DisplayName;
else 
   actionName = type.Name;
检查此链接:

使用反射,
typeof(YourController).GetMethod(“Login”).GetCustomAttributes()