GVillani82 GVillani82 - 3 months ago 34
ASP.NET (C#) Question

Trouble passing datetime parameter to web service, in GET

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

Answer

The problem is the colun (:) 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>