Search code examples
c#asp.net-web-apiswaggerazure-api-apps

Azure API App multiple actions with same verb and different routes gives swagger error


I have a ApiController with those GETs:

public class UsersController : ApiController
{
    public IHttpActionResult GetUsers()
    {
        [...]
    }

    public IHttpActionResult GetUsers(guid ID)
    {
        [...]
    }

    [Route("api/Users/{CodeA}/{CodeB}")]
    public IHttpActionResult GetUsers(string CodeA, string CodeB)
    {
        [...]
    }
}

The routing in webapiconfig.cs is the standard one:

config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}/",
            defaults: new { id = RouteParameter.Optional}
);

Trying to use swaggerUI I get a 500 error, and by fiddler I get:

Not supported by Swagger 2.0: Multiple operations with path 'api/utenti' and method 'GET'. See the config setting - \"ResolveConflictingActions\" for a potential workaround"

If I remove the last GET method swagger parses the api correctly. I've read from many sources that the problem is solved by specifying a different route, and I've tried to achieve this by adding the Route attribute to the last action.

Can someone please point me out in the right direction?

Thank you in advance.


Solution

  • The following code worked for me:

    [Route("users")]
    public class UsersController : ApiController
    {
        [Route("")]
        public IHttpActionResult GetUsers()
        {
            string test = "";
            return Ok(test);
        }
        [Route("{id}")]
        public IHttpActionResult GetUsers(Guid id)
        {
            string test = "";
            return Ok(test);
        }
    
        [Route("{CodeA}/{CodeB}")]
        public IHttpActionResult GetUsers(string CodeA, string CodeB)
        {
            string test = "";
            return Ok(test);
        }
    }