Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you have 2 GET methods with different parameter types within the same web api controller? [duplicate]

I have an asp.net web api controller with 2 GET methods in it. One accepts a string parameter and the other accepts an int parameter. I only have the default route that comes set up with web api in place.

        public HttpResponseMessage GetSearchResults(string searchTerm)
        {
            HttpResponseMessage response;
            //Do Work
            return response;
        }

        public HttpResponseMessage Get(int id)
        {
            HttpResponseMessage response;
            //Do Work
            return response;
        }

Every time I pass an int value in the URL, the GET method that takes the string parameter is called. The GET method that takes the int parameter is never called.

Is it possible to have 2 GET methods with different parameter types within the same controller?

-Edit- The suggested duplicate question is different because it asks about 2 methods with the exact same parameter types - I'm asking about different parameter types.

like image 836
Aaron Avatar asked Dec 14 '15 17:12

Aaron


People also ask

Can we have multiple get methods in Web API?

As mentioned, Web API controller can include multiple Get methods with different parameters and types. Let's add following action methods in StudentController to demonstrate how Web API handles multiple HTTP GET requests.

Can we have multiple get methods in controller?

Usually a Web API controller has maximum of five actions - Get(), Get(id), Post(), Put(), and Delete(). However, if required you can have additional actions in the Web API controller.

How do I pass multiple parameters to Web API controller methods?

You can pass parameters to Web API controller methods using either the [FromBody] or the [FromUri] attributes. Note that the [FromBody] attribute can be used only once in the parameter list of a method.

Can we have two methods with same name in controller?

To answer your specific question, you cannot have two methods with the same name and the same arguments in a single class; using the HttpGet and HttpPost attributes doesn't distinguish the methods.


1 Answers

Yes, it is possible. Out of the box with the default configuration, what you have should work assuming you are passing the searchTerm as a query string parameter. However, if you are attempting to pass it as part of the URL, like for example /api/myurl/blah, the default convention routing will attempt to match it with the int version of the method and return an error. You will either have to edit the default configuration or use Attribute Routing.

In general I find the convention based MVC routing to be less useful in WebApi so I usually disable it and use Attribute Routing.

To enable attribute routing, add

config.MapHttpAttributeRoutes();

to your WebApi config.

Then you can mark up your methods as such

[HttpGet]
[Route("api/myobject/")]
public HttpResponseMessage GetSearchResults(string searchTerm)
{
    HttpResponseMessage response;
    //Do Work
    return response;
}

[HttpGet]
[Route("api/myobject/{id:int}")]
public HttpResponseMessage Get(int id)
{
    HttpResponseMessage response;
    //Do Work
    return response;
}

Now, you can call the first method via

/api/myobject?searchTerm=blah

and the second via

/api/myobject/1

and they shouldn't collide.

However, if you want to have the searchTerm be in the URL instead of the query parameters, you can change the route to

[Route("api/myobject/{searchTerm}")]

The api/myobject/{id:int} route will catch all the ids and the api/myobject/{searchTerm} will catch most anything else. However, be careful with this as if the URL isn't URL encoded weird things will tend to happen.

I don't know precisely what URL formatting you are looking for so what I provided are just simple examples. The link I posted earlier delves deeper into attribute routing. It allows you to make for more complex routes than routing by convention that WebApi inherited from MVC.

like image 122
Bradford Dillon Avatar answered Sep 20 '22 05:09

Bradford Dillon