5

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.

Screenshot

Note that I simply created the project and added the method that is all I did. This is the browser error.

Screenshot

1
  • 1
    Maybe, as detailed in this answer, the missing information here is: you should not use the placeholder Name. Remove the placeholder and use the constructor with the string only or use the Template placeholder. Take care about the parameter amount, though. If you don't insert it inside the template, you'll only be able to pass it through QueryString. Commented Jun 22, 2023 at 21:25

4 Answers 4

7

Change [Route("[controller]")] to [Route("[controller]/[action]")]

Sign up to request clarification or add additional context in comments.

Comments

3

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();
}

Comments

2

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

Comments

1

I resolved this issue by adding route as below.

[Route("[controller]/[action]")] [! Redefine the route with respect to action.]1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.