Reputation: 213
When passing js Date objects to my ASP.NET Web Api controller, I always get null. I have tried passing strings, array of string, timespan - all those works, except Date. When inspecting the request, the date is passed like this:
date:"2014-03-13T15:00:00.000Z"
In angular:
$http({
method: 'get',
url: 'api/stuff',
params: {
date: new Date()
}
);
In my ApiController:
public IEnumerable<StuffResponse> Get(
[FromUri] DateTime? date
){ ... }
What is the correct way to pass dates?
Upvotes: 6
Views: 9647
Reputation: 148
This works for me:
$http({
method: 'get',
url: 'api/stuff',
params: {
date: $filter('date')(new Date(), "yyyy-MM-dd HH:mm:ss")
}
);
Upvotes: 13
Reputation: 4278
The formatting of the date in your Javascript is not compatible with .NET DateTime parsing. See here for valid formats.
http://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx
Upvotes: 1