Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the Attribute tag from a Controller Method in MVC?

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!

like image 243
Luke T Brooks Avatar asked Oct 12 '17 15:10

Luke T Brooks


People also ask

What is the use of action attribute in MVC?

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.

How to create an anchor tag in ASP NET MVC?

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.

How do I add a mvc5 controller to my MVC project?

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.

What are the attributes of display in ASP NET MVC?

Attributes in ASP.NET MVC Application 1 Display 2 DisplayName 3 DisplayFormat 4 ScaffoldColumn 5 DataTypeAttribute 6 DisplayColumnAttribute


1 Answers

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.

like image 84
NineBerry Avatar answered Sep 17 '22 17:09

NineBerry