Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass a date via the URL, for my Action to read in MVC?

How do I pass a date via the URL?

Here is what I am trying to do, as as you'll see the date uses forward slashes which are invalid in the URL

http://localhost/Controller/Action/id=0d5375aa-6d43-42f1-91f0-ea73d9beb361&date=02/12/2009
like image 220
Coppermill Avatar asked Dec 07 '09 10:12

Coppermill


2 Answers

The ISO 8601 standard is yyyy-MM-dd, which is unambiguous and does not contain any invalid URL characters, and works fine in DateTime.Parse/TryParse.

Another option is to use whatever format you want and simply encode the URL using HttpServerUtility.UrlEncode/UrlDecode.

like image 63
Aaronaught Avatar answered Nov 19 '22 09:11

Aaronaught


You could pass a date in the query string using a specific format, say yyyymmdd and then parse it correctly in your Controller.

&date=02/12/2009
change to
&date=20091202   (yyyymmdd)

You could either create a wrapper around the DateTime object that was instantiated using this new format or just parse it yourself in the Controller.

public MyWrapperDate(int date)
{
    int year = date / 10000;
    int month = ((date - (10000 * year)) / 100);
    int day = (date - (10000 * year) - (100 * month));

    this.DateTimeObject = new DateTime(year, month, day);
}
like image 35
David Avatar answered Nov 19 '22 08:11

David