QueryString Actions and Parameters in the Web API

I want my general route to determine if the query string was passed to Url like this

http://localhost/query/DailyLogs/1?dateOfLog='1/13/2013' 

Here is my current route definition:

 routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "query/{controller}/{id}", defaults: new { id = RouteParameter.Optional} ); 

I read several answers that say to add the value of dateOfLog as an optional action in the route definition:

 routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "query/{controller}/{id}/{dateOfLog}", defaults: new { id = RouteParameter.Optional, dateOfLog = RouteParameter.Optional } ); 

It doesn't work, maybe I'm doing something wrong, I'm not sure.


This is how I handle this problem right now, but I would like to use PowerBinding Power of Engine:

  var queryValue = Request.RequestUri.ParseQueryString(); string dateOfLog = queryValue["dateOfLog"]; 

Please tell me how to create a route definition that will use ModelBinding correctly and map your custom URL to controller parameters.

+6
source share
1 answer

In the controller action, simply include DateTime dateOfLog as a method parameter and continue to use the query string, because it will display just fine, the Web API will use the correct method overload if it finds it.

+6
source

All Articles