Home > Net >  How does Swagger achieve the same route but different query parameters?
How does Swagger achieve the same route but different query parameters?

Time:02-13

I created an asp.net core web api project, using the .net5 version, and I have a route like this.

[Route("api/detail")]
public IEnumerable<User> Get()
{
       //TODO
    return users;
}

[Route("api/detail")]
public IEnumerable<User> Get(string name)
{
        //TODO
    return users;
}

Although my request method is the same and the request parameters are different, the 500 error will be reported in swagger. Is there any way to solve it? Any help is greatly appreciated.

CodePudding user response:

There could be multiple reasons why you're getting a 500 error. When I pasted your code into a new controller the first is error I received was:

Ambiguous HTTP method for action... Actions require an explicit HttpMethod binding for Swagger

It's telling you that you need to decorate each action in the controller with an HttpMethod binding, like [HttpGet]. More on that in a second...

The next issue is that you're using [Route] to bind two different action methods to the exact same route with the same HttpMethod. That's not possible in an API controller.

Conflicting method/path combination... Actions require a unique method/path combination for Swagger

My preferred method for routing is to use swagger endpoints

  • Related