Trouble passing datetime parameter to web service, in GET
16,945
The problem is with the colon (:
) character in the URL which is not allowed. You can either put the parameter in the query string:
http://myserver/api/service/myusername/?lastUpdate=2008-12-10T12:30:00
Or disable the validation to check for potentially dangerous request paths by merging this to your web.config
:
<system.web>
<httpRuntime requestPathInvalidCharacters="" requestValidationMode="2.0" />
<pages validateRequest="false" />
</system.web>
Author by
GVillani82
Passionate about mobile development, clean code and reactive programming
Updated on July 31, 2022Comments
-
GVillani82 almost 2 years
I created a web service using ASP .NET Web API. The method is:
[HttpGet] [Route("service/{applicantUser}/{lastUpdate:datetime?}")] public IHttpActionResult getService(String applicantUser, DateTime? lastUpdate = null){ // some stuff here }
If I call the WS in this way:
http://myserver/api/service/myusername/2008-12-10
all works good, and I obtain a correct json file. But if I try to add the time:
http://myserver/api/service/myusername/2008-12-10T12:30:00
I obtain the error
404 - Bad Request
-
Rob Hardy over 9 yearsthis should be parsable using
Route("service/{applicantUser}#{lastUpdate:datetime?}")
-
DavidG over 9 yearsReally? Pretty sure anything after the
#
will not be passed to the server. -
Rob Hardy over 9 yearsYou're right! My mistake - Fortunately it seems you can also have a colon within a querystring which will be sent to the server, so that would be:
http://myserver/api/service/myusername?t=2008-12-10T12:30:00
-
GVillani82 over 9 yearsOk, thank you! Do you think it is really dangerous disable the validation?
-
DavidG over 9 yearsWell I suppose it's there for a very good reason. But, better to make up your own mind and read the documentation msdn.microsoft.com/en-us/library/hh882339(v=vs.110).aspx
-
anatol over 4 yearswhy do not use encoding for doing that stuff?
-
DavidG over 4 years@anatol Because that doesn't answer the question being asked.
-
anatol over 4 years@DavidG didn't get why not
-
DavidG over 4 years@anatol The question is about why the colon is causing problems, this answer tells you how to fix that. Whether you should pass the values encoded or not is a different question.
-
anatol over 4 years@DavidG I can't find any word about 'colon'. Anyway, could you answer - may it be kind of solution or not? I mean url encoding
-
DavidG over 4 years@anatol Of course it's a solution, but if you have a question, please ask one rather than commenting on other answers.
-
anatol over 4 years@DavidG I found your answer helpful, but still wonder why not encoding ) it is actual because encoding doesn't work for me, I'm getting 404
-
Enrico almost 4 yearsMM-dd-yyyyTHH-mm-s doesn't work, but thanks for trying :)