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?
Error
endpoint is used as the exception handler. Depending on the HTTP operation of the request, the according operation on theError
endpoint is called.services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "My Service", Version = "v1" }); }); app.UseSwagger(c => { c.PreSerializeFilters.Add((swagger, httpReq) => swagger.Host = httpReq.Host.Value); }); app.UseSwaggerUI(c => { c.RoutePrefix = "swagger"; // serve the UI at root c.SwaggerEndpoint("/swagger/v1/swagger.json", "V1 Docs"); });
ResolveConflictingActions
should work