I'm trying to get the Attribute tag from an API Controller so that I can see what HTTP verb it allows at runtime. From the sample code below, I want to be able to get the [HttpGet]
tag.
[HttpGet]
public void MyResource()
{
// Controller logic
}
I am currently using System.Reflection
to gather other information about my API while it is running, but so far I have been unable to retrieve the [HttpGet
tag and other Http
verb tags. I've tried each of the solutions below with no luck:
public void GetControllerMethodHttpAttribute()
{
MethodInfo controllerMethod = typeof(TestController).GetMethods().First();
// Solution 1
var attrs = controllerMethod.Attributes;
// Solution 2
var httpAttr = Attribute.GetCustomAttributes(typeof(HttpGetAttribute));
// Solution 3
var httpAttr2 = Attribute.GetCustomAttribute(controllerMethod, typeof(HttpGetAttribute));
// Solution 4
var httpAttr3 = Attribute.IsDefined(controllerMethod, typeof(HttpGetAttribute));
}
All of the previous questions that I've researched about this topic only related to Custom Attribute tags
and pulling values out of those, but I couldn't find any information about getting the framework-included Attribute tags
.
Does anyone know how I can get the [HttpGet]
Attribute tag?
Thanks!
This attribute can be used on action parameters and types, to indicate model level metadata. A base class for an MVC controller with view support. Information about the current routing path. Adds endpoints for controller actions to the IEndpointRouteBuilder and specifies a route with the given name, pattern, defaults, constraints, and dataTokens.
Anchor Tag Helper In ASP.NET MVC. The Anchor Tag Helper generates HTML anchor (<a> </a>) element by adding a new attribute. The "href" attribute of the anchor tag is created by using new attributes. The Anchor Tag Helper generates an HTML anchor (<a> </a>) element by adding new attribute.
Right-click the Controllers folder and add a controller. A window will appear. Choose MVC5 Controller-Empty and click "Add". After clicking on "Add", another window will appear with DefaultController. Change the name to HomeController and click "Add". The HomeController will be added under the Controllers folder.
Attributes in ASP.NET MVC Application 1 Display 2 DisplayName 3 DisplayFormat 4 ScaffoldColumn 5 DataTypeAttribute 6 DisplayColumnAttribute
The proposed solutions 3 and 4 work.
You must look out that you are referencing the correct HttpGetAttribute
. There is one in System.Web.Http.HttpGetAttribute
and there is one in System.Web.Mvc.HttpGetAttribute
.
The following code lists the public methods of the ValuesController
API controller with the information on whether they have HttpGet or HttpPost attributes.
var methods = typeof(ValuesController).GetMethods();
string infoString = "";
foreach(var method in methods)
{
// Only public methods that are not constructors
if(!method.IsConstructor && method.IsPublic)
{
// Don't include inherited methods
if(method.DeclaringType == typeof(ValuesController))
{
infoString += method.Name;
if(Attribute.IsDefined(method, typeof(System.Web.Http.HttpGetAttribute)))
{
infoString += " GET ";
}
if(Attribute.IsDefined(method, typeof(System.Web.Http.HttpPostAttribute)))
{
infoString += " POST ";
}
infoString += Environment.NewLine;
}
}
}
You need to exchange System.Web.Http.
with System.Web.Mvc.
when the controller is an MVC controller instead of an API controller.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With