I just need it, and since there was no answer to the actual requirement of Web Api attributes, I sent my answer.
The attributes of Web Api are as follows:
- System.Web.Http.HttpGetAttribute
- System.Web.Http.HttpPutAttribute
- System.Web.Http.HttpPostAttribute
- System.Web.Http.HttpDeleteAttribute
Unlike their Mvc copies, they are not inherited from the base attribute type, but inherited directly from System.Attribute. Therefore, you need to manually check each specific type separately.
I made a small extension method that extends the MethodInfo class as follows:
public static IEnumerable<Attribute> GetWebApiMethodAttributes(this MethodInfo methodInfo) { return methodInfo.GetCustomAttributes().Where(attr => attr.GetType() == typeof(HttpGetAttribute) || attr.GetType() == typeof(HttpPutAttribute) || attr.GetType() == typeof(HttpPostAttribute) || attr.GetType() == typeof(HttpDeleteAttribute) ).AsEnumerable(); }
After you have a MethodInfo object for your controller action method by reflection, calling the above extension method will give you all the action method attributes currently used for this method:
var webApiMethodAttributes = methodInfo.GetWebApiMethodAttributes();
source share