Home > Enterprise >  adding new method to controller makes swagger give 500 error
adding new method to controller makes swagger give 500 error

Time:09-05

so it's been a while since i made an api. Now i was looking at it again and i tried to simply add a get method to the WeatherForecastController. But i keep getting a swagger error after adding it. the method looks very similar to the existing method. only that this one uses a parameter. but after adding it i keep getting an error in my browser. and i'm not sure why. i haven't done this in a very long time. and i never used swagger before. enter image description here

note that i simply created the project and added the method that is all i did. this is the browser error.

enter image description here

CodePudding user response:

Try this;

[HttpGet("GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
    return Enumerable.Range(1, 5).Select(index => new WeatherForecast
    {
        Date = DateTime.Now.AddDays(index),
        TemperatureC = Random.Shared.Next(-20, 55),
        Summary = Summaries[Random.Shared.Next(Summaries.Length)]
    })
    .ToArray();
}

[HttpGet("GetNumberOfForecasts/{amount}")]
public IEnumerable<WeatherForecast> GetAmount(int amount)
{
    return Enumerable.Range(1, amount).Select(index => new WeatherForecast
    {
        Date = DateTime.Now.AddDays(index),
        TemperatureC = Random.Shared.Next(-20, 55),
        Summary = Summaries[Random.Shared.Next(Summaries.Length)]
    })
    .ToArray();
}

CodePudding user response:

Just for future reference.

Step 1:-> Open your dev tools, or just F12.

      Then open the network tab, and refresh your page.

enter image description here

Step 2:> Click on the swagger call and click on the response tab.

You will see your error:

Swashbuckle.AspNetCore.SwaggerGen.SwaggerGeneratorException: Conflicting method/path combination "GET WeatherForecast" for actions - WebApplication1.Controllers.WeatherForecastController.Get (WebApplication1),WebApplication1.Controllers.WeatherForecastController.GetAmount (WebApplication1). Actions require a unique method/path combination for Swagger/OpenAPI 3.0. Use ConflictingActionsResolver as a workaround

enter image description here

Try routing like this

enter image description here

Results

enter image description here

  • Related