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

14,595

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.

Share:
14,595

Related videos on Youtube

Aaron
Author by

Aaron

Updated on September 14, 2022

Comments

  • Aaron
    Aaron over 1 year

    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.

  • Aaron
    Aaron over 8 years
    Excellent explanation - attributed routing worked perfectly!
  • Bradford Dillon
    Bradford Dillon over 8 years
    No problem. Glad it helped.
  • Al-Hanash Moataz
    Al-Hanash Moataz over 3 years
    nice usefull answer , thanks +1