6

I am working on a REST API with ASP.NET Web API 2. Thing get bad when I try to integrate dates into the game.

Here is the route:

[Route("{id:Guid}/{from:DateTime}/{to:DateTime}")]

When I do that I can perfectly pass something like

.../[id]/2012-01-01/2013-01-01

However, when I get to the point where I need the time information it gets quite bad, let's imagine this one:

.../[id]/2012-01-01/2013-01-01 1:45:30 PM/2013-01-01 1:45:30 PM

It seems like the spaces are going OK but the ":" are blocking. So I though I should use my own format, being yyyyMMddhhmm. This gives the following URL:

.../[id]/201301031147/201401031147

However, .NET is not expecting this as a DateTime and does not know how to use it. So I used a IHttpRouteConstraint to allow it. The problem is that it still does not know how to deal with it after I told it it's fine...

So my question is, how do you pass a DateTime to the route?

Alex Angas
60.2k41 gold badges145 silver badges213 bronze badges
asked Jan 3, 2014 at 12:13
7
  • 1
    Did you try to encode your request url? For instance, if you encode this string 2013年01月01日 1:45:30 PM/2013-01-01 1:45:30 PM you will get this one 2013年01月01日%201%3A45%3A30%20PM%2F2013-01-01%201%3A45%3A30%20PM Commented Jan 3, 2014 at 12:18
  • Hello, thanks for the reply! I actually use RestSharp on the client side and it does that automatically. Commented Jan 3, 2014 at 12:30
  • All right. What about making your date string compatible with The Universal Sortable Format. Could you check if that works? Here is a sample date string represented in that format: 2008年04月10日 13:30:00Z Commented Jan 3, 2014 at 12:49
  • Thanks again! I tried u, o and s formats described in msdn ( msdn.microsoft.com/fr-fr/library/zdtaw1bw(v=vs.110).aspx ) The one you're refeering to is u. No luck this way either sadly... I think there must be a place to tell the framework which format to use or something like that. Or at least a way to take that data and transform it myself into a DateTime Commented Jan 3, 2014 at 12:57
  • 1
    One approach to overcome this problem is to convert the datetime object to UTC at the client, and back to datetime from UTC in the webapi. Commented Mar 28, 2014 at 11:45

1 Answer 1

4

The problem is that : is a URL reserved character in the path. MVC doesn't expect it to be URL encoded and won't handle it. However, it is not reserved in the query string.

Remove it from your routing specification, but leave them as parameters in your method and the model binder will bind them. Alternatively, you can also remove them as parameters and access them via the Context query string property or ControllerContext.Request.GetQueryNameValuePairs().

So you would have ?fromDate=2012年01月01日T1:45:30PM&toDate=2013年01月01日T1:45:30PM

answered Aug 18, 2015 at 3:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.