0

I work on my web api project. I get error 404 when I try to pass date variable in get request to web api method.

Here is web api method:

[HttpGet]
[Route("ReportDetailed/{clientId}/{date}")]
public IHttpActionResult ReportDetailed(int clientId, DateTime date )
{
    return Ok();
}

And here how I try to access to the function above(row from URL):

http://localhost/station/api/Reports/ReportDetailed/12/Wed%20Jun%2001%202016%2000:00:00%20GMT+0300%20(Jerusalem%20Daylight%20Time)

where clientId is 12 and date is parsed javascript date object:

/Wed%20Jun%2001%202016%2000:00:00%20GMT+0300%20(Jerusalem%20Daylight%20Time)

when I remove from URL above the parsed Date object and date parameter from web api function it works perfect (so I guess the problem with date object).

Any idea why I get error 404 when I try to pass object in URI?

2
  • 1
    What if you try a more simplified date, eg: /ReportDetailed/12/2016-06-06? Does that work? If so, it's the model binder for date not recognising your date - you could use a string param and parse it in your action (throwing an exception if you can't parse it) Commented Jun 6, 2016 at 9:47
  • @freedomn-m thank you. Your comment helped. Commented Jun 6, 2016 at 11:19

2 Answers 2

1

Your provided date is not in a valid date format. Try a more universally parse-able format like YYYY-MM-dd HH:mm:ss for example.

You should also use Route constraints on your parameters which will try to validate your parameters to make sure they are in the correct format

[HttpGet]
[Route("ReportDetailed/{clientId:int}/{date:datetime}")]
public IHttpActionResult ReportDetailed(int clientId, DateTime date )
{
    return Ok();
}
Sign up to request clarification or add additional context in comments.

Comments

1

Date format is not recognizable by .NET First convert your date to UTC string, this will return a string representation of your date. So the value will be passes as string

var dateString= dateJs.toUTCString();

Then in your C# code parse it.

public IHttpActionResult ReportDetailed(int clientId, string dateString)
    DateTime dateC# = DateTime.Parse(dateString);

8 Comments

Katana, when I try to parse toUTCString() I get wrong date: "Tue, 31 May 2016 21:00:00 GMT"
@Michael I think you are creating a new date instance. Use the value you pasted above. Michael is the error in the API gone?
@Michael sorry i forgot to mention that the output of toUTCString is a string so need to change your datetime parameter in API to string
I use toISOString()
I get date undefined in the action
|

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.