ASP.NET Core - Swashbuckle.AspNetCore.SwaggerGen.SwaggerGeneratorException: Ambiguous HTTP method for action
I’m currently rewriting an existing ASP.NET Web API application to ASP.NET Core.
I ported a controller from ASP.NET Web API to ASP.NET Core. Here is the end result:
[ApiController] | |
[Route("[controller]")] | |
public class AuthTypeController : ControllerBase | |
{ | |
private readonly IAuthTypeService _authTypeService; | |
public AuthTypeController(IAuthTypeService authTypeService) | |
{ | |
_authTypeService = authTypeService; | |
} | |
[HttpGet("{id}")] | |
[ResponseCache(Duration =3600, Location = ResponseCacheLocation.Any)] | |
public async Task<ActionResult<AuthType>> GetAuthTypeById(int ID) | |
{ | |
var authType = await _authTypeService.FindById(ID); | |
if (authType == null) | |
{ | |
return NotFound(); | |
} | |
return authType; | |
} | |
public async Task<ActionResult<IList<AuthType>>> GetAll() | |
{ | |
var authTypes = await _authTypeService.FindAll(); | |
return new List<AuthType>(authTypes); | |
} | |
} | |
Everything looked OK at first sight, but when I tried to run the application I got the following error message:
Swashbuckle.AspNetCore.SwaggerGen.SwaggerGeneratorException: Ambiguous HTTP method for action - Controllers.AuthTypeController.GetAll (VLM.IAM.Services). Actions require an explicit HttpMethod binding for Swagger/OpenAPI 3.0
at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOperations(IEnumerable`1 apiDescriptions, SchemaRepository schemaRepository)
at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GeneratePaths(IEnumerable`1 apiDescriptions, SchemaRepository schemaRepository)
at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwagger(String documentName, String host, String basePath)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContextOfT`1.ProcessRequestAsync()
The Swagger/OpenAPI middleware requires that you explicitly annotate all your action methods. So I had to update the GetAll action method and annotate it with a [HttpGet] attribute:
[HttpGet] | |
public async Task<ActionResult<IList<AuthType>>> GetAll() | |
{ | |
var authTypes = await _authTypeService.FindAll(); | |
return new List<AuthType>(authTypes); | |
} |