cda*_*a01 7 reflection asp.net-mvc custom-attributes asp.net-web-api
我有一个ASP.NET Web API项目.使用反射,我如何获得[HttpGet]装饰我的动作方法的Http动词(在下面的例子中)属性?
[HttpGet]
public ActionResult Index(int id) { ... }
Run Code Online (Sandbox Code Playgroud)
假设我的控制器中有上述操作方法.到目前为止,通过使用反射,我已经能够获得Index动作方法的MethodInfo对象,该对象存储在一个名为的变量中methodInfo
我尝试使用以下内容获取http动词,但它不起作用 - 返回null:
var httpVerb = methodInfo.GetCustomAttributes(typeof (AcceptVerbsAttribute), false).Cast<AcceptVerbsAttribute>().SingleOrDefault();
Run Code Online (Sandbox Code Playgroud)
我发现的东西:
我上面的例子来自我正在研究的ASP.NET Web API项目.
看来这[HttpGet]是一个System.Web.Http.HttpGetAttribute
但在常规的ASP.NET MVC项目中,它[HttpGet]是一个System.Web.Mvc.HttpGetAttribute
var methodInfo = MethodBase.GetCurrentMethod();
var attribute = methodInfo.GetCustomAttributes(typeof(ActionMethodSelectorAttribute), true).Cast<ActionMethodSelectorAttribute>().FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
你很近...
区别在于所有“动词”属性都继承自“ ActionMethodSelectorAttribute ”,包括“ AcceptVerbsAttribute ”属性。
| 归档时间: |
|
| 查看次数: |
2793 次 |
| 最近记录: |