Creating a route that can accept a DateTime in a URI with an asp.net web api 2 attribute routing

I'm trying to figure out if I need to write a custom IHttpRouteConstraint , or if I can struggle with the built-in to get what I want. I don’t see anything to find good documentation.

Basically, here is my action:

 [Route("var/{varId:int:min(1)}/slot/{*slot:datetime}")] public async Task<HttpResponseMessage> Put(int varId, DateTime slot) { ... } 

I want it to be called like this: PUT /api/data/var/1/slot/2012/01/01/131516 and bind the framework 19 to var id and a DateTime with the value "January 1, 2012, 1:15 : 16 pm "as the value of" slot ".

Following the guide from here: http://www.asp.net/web-api/overview/web-api-routing-and-actions/create-a-rest-api-with-attribute-routing I can make it work by passing only date segments i.e. PUT /api/data/var/1/slot/2012/01/01 PUT /api/data/var/1/slot/2012-01-01 or PUT /api/data/var/1/slot/2012-01-01 , but that only gives me the data value, no temporary components.

Something tells me that trying to get in time in any reasonable way through the URI segments is a bad idea, but I'm not sure why this would be a bad idea, besides the ambiguity about local versions of UTC.

I also tried to limit the DateTime constraint to a regular expression, e.g. {slot:datetime:regex(\\d{4}/\\d{2}/\\d{2})/\\d{4})} to try to get it to parse something like 2013/01/01/151617 as DateTime, but to no avail.

I'm sure I can get this to work with a custom IHttpRouteConstraint , I just don't want to do something that can be embedded.

Thank!

0
asp.net-web-api asp.net-web-api-routing
Oct 30 '13 at 19:48
source share
2 answers

Limiting the time to use the web API does nothing special in analyzing the date and time, as you can see below (source code here ). If your request url is like var/1/slot/2012-01-01 1:45:30 PM or var/1/slot/2012/01/01 1:45:30 PM , it works fine ... but I think if you need complete flexibility, then creating a custom constraint is the best option ...

 public bool Match(HttpRequestMessage request, IHttpRoute route, string parameterName, IDictionary<string, object> values, HttpRouteDirection routeDirection) { if (parameterName == null) { throw Error.ArgumentNull("parameterName"); } if (values == null) { throw Error.ArgumentNull("values"); } object value; if (values.TryGetValue(parameterName, out value) && value != null) { if (value is DateTime) { return true; } DateTime result; string valueString = Convert.ToString(value, CultureInfo.InvariantCulture); return DateTime.TryParse(valueString, CultureInfo.InvariantCulture, DateTimeStyles.None, out result); } return false; } 
+3
Oct 30 '13 at 20:59
source share

parameter must pass DateTime as query string parameters (see [FromUri]

eg.

 [Route("api/Customer/{customerId}/Calls/")] public List<CallDto> GetCalls(int customerId, [FromUri]DateTime start, [FromUri]DateTime end) 

it will have a signature

 GET api/Customer/{customerId}/Calls?start={start}&end={end} 

Create query string dates with

 startDate.ToString("s", CultureInfo.InvariantCulture); 
Query string

will look like

 api/Customer/81/Calls?start=2014-07-25T00:00:00&end=2014-07-26T00:00:00 
+2
Jul 25 '14 at 12:48
source share



All Articles