How to pass UTC dates to Web API?
Passing 2010-01-01
works fine, but when I pass a UTC date such as 2014-12-31T22:00:00.000Z
(with a time component), I get a HTTP 404 response. So
http://domain/api/controller/action/2012-12-31T22:00:00.000Z
yields a 404 error response, while
http://domain/api/controller/action/2012-12-31
works fine.
How to pass UTC dates to Web API then - or at least specify date and time?
The problem is twofold:
.
in the routeBy default, IIS treats all URI's with a dot in them as static resource, tries to return it and skip further processing (by Web API) altogether. This is configured in your Web.config in the section system.webServer.handlers
: the default handler handles path="*."
. You won't find much documentation regarding the strange syntax in this path
attribute (regex would have made more sense), but what this apparently means is "anything that doesn't contain a dot" (and any character from point 2 below). Hence the 'Extensionless' in the name ExtensionlessUrlHandler-Integrated-4.0
.
Multiple solutions are possible, in my opinion in the order of 'correctness':
path="*."
attribute to path="*"
. It will then catch everything. Note that from then on, your web api will no longer interpret incoming calls with dots as static resources! If you are hosting static resources on your web api, this is therefor not advised!<system.webserver>
: <modules runAllManagedModulesForAllRequests="true">
:
in the routeAfter you've changed the above, by default, you'd get the following error:
A potentially dangerous Request.Path value was detected from the client (:).
You can change the predefined disallowed/invalid characters in your Web.config. Under <system.web>
, add the following: <httpRuntime requestPathInvalidCharacters="<,>,%,&,*,\,?" />
. I've removed the :
from the standard list of invalid characters.
Although not an answer to your question, a safer and easier solution would be to change the request so that all this is not required. This can be done in two ways:
?date=2012-12-31T22:00:00.000Z
..000
from every request. You'd still need to allow :
's (cfr point 2).