When building a REST API, it’s recommended to annotate the controller actions with the available HTTP attributes to be precise about what your API supports.
You should annotate the controller actions with the available HttpMethod attributes. You can still use them in conjunction with the Route attribute, in case there are multiple templates for one action and you need to set the order. This allows you to clearly define the HTTP methods each action method should respond to, while still being able to customize your routes.
This rule does not raise if the controller or the action is annotated with [ApiExplorerSettings(IgnoreApi =
true)] or AcceptsVerbs
attribute.
[Route("Customer")] // This route conflicts with GetCustomers action route
public async Task<IResult> ChangeCustomer([FromBody] CustomerData data) // Noncompliant
{
// ...
return Results.Ok();
}
[Route("Customer")] // This route conflicts with ChangeCustomer action route
public async Task<string> GetCustomers() // Noncompliant
{
return _customerRepository.GetAll();
}
[Route("Customer")]
[HttpPost]
public async Task<IResult> ChangeCustomer([FromBody] CustomerData data) // Compliant
{
// ...
return Results.Ok();
}
[HttpGet("Customer")]
public async Task<string> GetCustomers() // Compliant
{
return _customerRepository.GetAll();
}