【发布时间】:2012-05-30 14:20:17
【问题描述】:
我有一个 ASP.NET Web API 项目。使用反射,如何获得装饰我的操作方法的 Http 动词(下例中的[HttpGet])属性?
[HttpGet]
public ActionResult Index(int id) { ... }
假设我的控制器中有上述操作方法。到目前为止,通过使用反射,我已经能够获得 Index 操作方法的 MethodInfo 对象,我将其存储在一个名为 methodInfo 的变量中
我尝试使用以下方法获取 http 动词,但没有成功 - 返回 null:
var httpVerb = methodInfo.GetCustomAttributes(typeof (AcceptVerbsAttribute), false).Cast<AcceptVerbsAttribute>().SingleOrDefault();
我注意到的一点:
我上面的示例来自我正在处理的一个 ASP.NET Web API 项目。
看来[HttpGet]是一个System.Web.Http.HttpGetAttribute
但在常规 ASP.NET MVC 项目中,[HttpGet] 是 System.Web.Mvc.HttpGetAttribute
【问题讨论】:
标签: asp.net-mvc reflection asp.net-web-api custom-attributes