1

In Web API 2, I can put constraints in the route template; for example:

[Route("api/foos/{id:int}")]
void GetFooWithId(int id, [FromUri] string format = null)
{ ... }

Is there a way to constrain query string parameters, such as format in the above example, in a similar manner?

(I know that I could perhaps declare a custom value type for the format parameter and then define a [TypeConverter] on it that fails for invalid values, but I would first like to see whether Web API 2's route constraint mechanism can be employed for query string parameters, too.)

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
  • Possible duplicate of [Query parameter route constraints](http://stackoverflow.com/questions/21589011/query-parameter-route-constraints) – stakx - no longer contributing Aug 15 '16 at 09:03
  • interesting question .., Did you get the answer for this. please let us know as well. thanks.! – eRaisedToX Oct 03 '17 at 11:42
  • @eRaisedToX: I think the (unspoken) answer is that Web API 2 doesn't support this out of the box (I'm almost sure it could be done via some extension point somehow), but I've stopped researching this topic. Perhaps it's looking different with ASP.NET Core MVC. – stakx - no longer contributing Oct 04 '17 at 14:10

0 Answers0