Search code examples
c#annotationsdata-annotationsasp.net-web-api2asp.net-web-api-routing

What is the advantage of adding the [HttpGet] annotation?


In the event that a Controller specifies a route:

[Route("api/platypus/getPlatypi")]
public List<Platypus> GetAllPlatypi()
{
    return _platypusRepository.GetPlatypi();
}

...is there any advantage to annotating it with a "[HttpGet]" like so:

[HttpGet]
[Route("api/platypus/getPlatypi")]
public List<Platypus> GetAllPlatypi()
{
   return _platypusRepository.GetPlatypi();
}

?


Solution

  • For the example you have given there is not any advantage to adding the HTTP method attribute. By convention Web API will try to match a controller method that starts with the HTTP request method (GET, POST, PUT etc.).

    In your example the method GetAllPlatypi will be considered for a match for all GET requests to that controller.

    If however your method was named FindAllPlatypi you would need to add the [HttpGet] attribute to make it clear that this method is meant for GET requests.