1

I create this date variable in client side:

 this.latestDate = new Date(2001, 1, 1,1,1,1,1);

Here how it looks in client watch:

self.latestDate: Thu Feb 01 2001 01:01:01 GMT+0200 (Jerusalem Standard Time)

here is angularjs service that I use to asyncroniusly call my web api method:

$http.get(serviceUrl + "?date=" + self.latestDate);

And here is the web api method that I call from cilent:

    [HttpGet]
    public HttpResponseMessage GetByDate(DateTime date){}

But each time I call for web api method above, I get this error on cilent:

<Error>
<Message>The request is invalid.</Message>
<MessageDetail>
The parameters dictionary contains a null entry for parameter 'date' of non-nullable type 'System.DateTime' for method 'System.Net.Http.HttpResponseMessage GetByDate(System.DateTime)' in 'SensorObservation.Web.SensorsDataController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
</MessageDetail>
</Error>

Any idea why I get the error above?

1 Answer 1

1

First, doing this is a really bad idea:

$http.get(serviceUrl + "?date=" + self.latestDate);

A URL is a structured text format; it has formatting rules that must be followed. When you dump unstructured text into a structured format by using simple string concatenation, you are likely to be breaking some of those rules. I would recommend using the jQuery param function for this.

Second, you are serializing the date to a string using the default format, which is the one you see in the client watch. This may or may not work on the server. A better bet is to serialize using a well known format such as ISO 8601. I would recommend using the JavaScript date toISOString function for this.

Applying these changes, your API call code would look something like this:

var query = jQuery.param({ date: self.latestDate.toISOString() });
$http.get(serviceUrl + "?" + query);

Update

I ran a quick test since I was in my WebAPI code already, and you can create and use an endpoint with a DateTime parameter as long as the URL is formatted correctly.

My test endpoint looks like this:

[HttpGet]
[Route("test/datetest")]
public HttpResponseMessage DateTest(DateTime d)
{
    HttpResponseMessage response = new HttpResponseMessage();
    response.Content = new StringContent(d.ToString(), Encoding.UTF8, "text/plain");
    return response;
}

It echoes the date back as expected.

Sign up to request clarification or add additional context in comments.

2 Comments

I tryed to use UTC like that new Date(Date.UTC(2001, 1, 1,1,1,1,1)). But it didnt work
@Michael That isn't the same as what I'm suggesting. Using Date.UTC has nothing to do with how the date gets serialized as a string. If you haven't yet, be sure to look at the URL that is being sent to the server. That might make it clearer. You can also try different things by manually loading the URL in a browser or using CURL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.