Search code examples
asp.netroutesasp.net-mvc-routingasp.net-apicontroller

ApiController routing with non-existing parameters


I have an ApiController like this:

SearchController : ApiController {
  public Book Get(int booksn=-1, string author="")
  {
    /* search for books matching the parameters */
  }
}

All parameters have default value and an empty search request returns everything.

Now my problem is that if the request contains other parameters (for example typo from the API user) it will still route to this function :

GET /search?booksn=3&hello=world

The underlying problem is that if a parameter is sent with a wrong type it will route the same way and I wont get the parameter value in the method so I can't even send an error response:

GET /search?booksn=helloWorld

Is there a way to prevent this? Either prevent wrong types, or prevent all undefined parameters? (I have also tried with an object and the [FromUri] attribute but same problem)

Edit: I am using VS 2012 with .NET framework 4


Solution

  • I ended up using string params even for number, using int.Parse to throw an error if the value isn't correct.