Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swashbuckle/Swagger + ASP.Net Core: "Failed to load API definition"

I develop an ASP.NET Core 2 application and included Swagger. Everything worked fine until I introduced a method without explicitly defining the HTTP action:

public class ErrorController : Controller
{
    [Route("/error")]
    public IActionResult Index()
    {
        return StatusCode(500, new Error("Internal error."));
    }
}

When I started the app with this method, the following message showed up:

Failed to load API definition.

Errors
Fetch error Internal Server Error /swagger/v1/swagger.json

As soon as I explicitly set e.g. [HttpGet] the error disappears. The problem with this is, I need this method to fire for all possible HTTP operations. Of course, I could specify all operations explicitly, but I have the feeling Swagger should be able to handle this correctly.

Why does Swagger behave this way?

Is there any configuration I can use?

like image 388
Ash Avatar asked May 07 '18 07:05

Ash


3 Answers

Add Httpxxx([HttpGet], [HttpPost], ...) attribute for each Action method, or [ApiExplorerSettings(IgnoreApi = true)]

like image 167
LiKui Avatar answered Sep 22 '22 06:09

LiKui


Simply you can look into the log in the Output window. The actual error can be seen there in my case, I missed adding HTTP action on top of a methods

enter image description here

like image 37
Hossein Narimani Rad Avatar answered Sep 23 '22 06:09

Hossein Narimani Rad


The option ResolveConflictingActions should be working on this case...

Here is the actual error:

System.NotSupportedException: Ambiguous HTTP method for action

That is coming from: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/86cc761bc4f5dda796f80ad8dfd8bc205542a6f6/src/Swashbuckle.AspNetCore.SwaggerGen/Generator/SwaggerGenerator.cs#L90

I think this is a bug, if you are truly interested you should report it to the project

like image 37
Helder Sepulveda Avatar answered Sep 20 '22 06:09

Helder Sepulveda