Error passing Datetime as querystring parameter to Servicestack.net GET method
-
12-11-2019 - |
문제
I trying to pass a DateTime object as a query string parameter to a webservice method built with ServiceStack.net.
The date is properly URL encoded when passed but I keep getting the following error:
"Message":"KeyValueDataContractDeserializer: Error converting to type: String was not recognized as a valid DateTime.
Any ideas of how to correct this?
Here is an example of the URL encoded DateTime I was passing: 2%2f24%2f2012+5%3a13%3a02+PM
해결책
If you have to pass datetimes as strings, try to pass them in an unambiguous format. I assume you're tying to pass the 24th February 2012, 17:13:02 - something like the following would be unambiguous:
2012-02-24T17:13:02
E.g. if you're formatting the date in .NET, use "o" or "s"
This is nothing to do with URL encoding - it's to do with passing data unambiguously - dates such as 01/02/2012 can be interpreted at least two ways - 1st February or 2nd January of this year, for two.
다른 팁
Have you looked at having the websservice accept a string parameter and then using DateTime.Parse or DateTime.TryParse to convert the string to a DateTime?