How Do I Use a Date (in the MM-dd-yyyy format) in a URL in an MVC Application?
I have an MVC2 project that has a route in the RegisterRoutes method as follows:
routes.MapRoute(
"PrettyUrl",
"Airplanes/{query}/{page}",
new { controller = "Airplanes", action = "Details", query ="", page = 0 }
);
Currently, the URL for a GET looks like Airpla开发者_开发技巧nes/va123-va234/1
- I would like to add a date like: Airplanes/va123-va234/10-10-2010/1
but I can't figure out how to get the route to map the date to this format.
You can add a parameter for date
routes.MapRoute(
"PrettyUrl",
"Airplanes/{query}/{date}/{page}",
new { controller = "Airplanes", action = "Details", query ="", page = 0,
date = UrlParameter.Optional }
);
In your controller method, make sure you take in a date
parameter.
public ActionResult Details(string query, string date, int page) {
var newDate = DateTime.Parse(date);
return View();
}
精彩评论