Stacker
Stacker

Reputation: 8237

Pass Id and String to WebAPI Get

Im using the New MVC Web Api I have something like this:

    public class CustomersController : ApiController
    {
  public HttpResponseMessage Get()
        {
            //Return Somethings
        }
    }

I call it with /api/customers

now i want to be able to filter by nationality and id so i made this :

public class CustomersController : ApiController
{
    public HttpResponseMessage Get(int Id, String Nat)
    {
        //and i filter here Return Somethings
    }
}

if i try to use /api/customers/1/us it call the first method, i understand that these are query strings, so how do i define route so i can use /api/customers/1/us ?

Upvotes: 0

Views: 12614

Answers (1)

Claudio Redi
Claudio Redi

Reputation: 68400

Something like this should do the trick

routes.MapHttpRoute(
     name: "CustomersByIdAndNat",
     routeTemplate: "api/customers/{Id}/{Nat}/",
     defaults: new { controller = "Customers" }
);

And as a side note, /1/us is not the querystring. Querystring is what it comes after ? symbol on urls. For instance in your case, could be something like this

api/customers/?id=1&nat=us

Not saying you need to change the url format, just explaining the concept.

Upvotes: 4

Related Questions