简体   繁体   中英

Passing Date in AngularJS $http get to ASP.NET Web Api

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?

This works for me:

$http({ 
    method: 'get', 
    url: 'api/stuff', 
    params: {
       date: $filter('date')(new Date(), "yyyy-MM-dd HH:mm:ss")
    }
);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM