asp.net-web-apiroutesquery-stringattributerouting

ASP.NET Web Api attribute routing and query string


I've specified my routing like this:

[RoutePrefix("users")]
public class UsersController : ApiController
{
    [ResponseType(typeof(List<User>))]
    [Route("")]
    public IHttpActionResult GetAll()
    {

    }

    [Route("{birthdate}")]
    [ResponseType(typeof(List<User>))]
    public IHttpActionResult GetByBirthdate(DateTime birthdate)
    {

    }

But when I am using this url: localhost/Users?birthdate=1907-04-19&api-version=2.0

I get redirected to GetAll() method. Why is that?


Solution

  • localhost/Users?birthdate=1907-04-19&api-version=2.0
    

    that mean you call url users with param birthdate

    If you want to go to second you need use

    http://localhost/users/birthdate?birthdate=1907-04-19