开发者

How Do I Use a Date (in the MM-dd-yyyy format) in a URL in an MVC Application?

开发者 https://www.devze.com 2023-01-27 07:03 出处:网络
I have an MVC2 project that has a route in the RegisterRoutes method as follows: routes.MapRoute( \"PrettyUrl\",

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();
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消