admin管理员组

文章数量:1240583

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?

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?

Share Improve this question edited Apr 10, 2014 at 19:36 andyp 6,2693 gold badges40 silver badges55 bronze badges asked Mar 3, 2014 at 14:22 TryllingTrylling 2132 gold badges4 silver badges9 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 13

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 patible with .NET DateTime parsing. See here for valid formats.

http://msdn.microsoft./en-us/library/az4se3k1(v=vs.110).aspx

本文标签: cPassing Date in AngularJS http get to ASPNET Web ApiStack Overflow