SwaggerGeneratorException: Ambiguous HTTP method for action error

Viewed 48

I have the below controller (testing purposes, it's not following REST with 2 GETs):

[ApiController]
[Route("api/[controller]")]
public class WeatherForecastController : ControllerBase
{
    private readonly ILogger<WeatherForecastController> _logger;
    private readonly IWeatherService _weatherService;

    public WeatherForecastController(ILogger<WeatherForecastController> logger, IWeatherService weatherService)
    {
        _logger = logger;
        _weatherService = weatherService;
    }

    [HttpGet(Name = "GetWeatherSummary")]
    [Route("getweatherforecast")]
    public IEnumerable<WeatherForecast> Get()
    {
        return _weatherService.GetWeatherSummary();
    }


    [HttpGet(Name = "Error")]
    [Route("error")]
    public Task<IActionResult> Error()
    {
        throw new Exception("some error message");
    }
}

I have 2 GET methods in there, both not taking any params. I tried adding a Route to each one and Name as well, but Swagger still shows the error:

Swashbuckle.AspNetCore.SwaggerGen.SwaggerGeneratorException: Conflicting method/path combination "GET api/WeatherForecast"

I'm able to hit both in postman using:

http://localhost:5000/api/WeatherForecast/getweatherforecast

http://localhost:5000/api/WeatherForecast/error

Does swagger not allow this, as they are both GET and both don't take any params, so it can't distinguish between them? the Name or Route is not enough?

1 Answers

You could try

[HttpGet(Name = "GetWeatherSummary")]
    
    public IEnumerable<WeatherForecast> Get()
    {
        ......
    }


    [HttpGet(Name = "Error")]
    
    public Task<IActionResult> Error()
    {
        ......
    }

Or

        [HttpGet]
        [Route("getweatherforecast")]
        public IEnumerable<WeatherForecast> Get()
        {
            ......
        }
    
    
        [HttpGet]
        [Route("error")]
        public Task<IActionResult> Error()
        {
            ......
        }
Related