如何处理包含正斜杠的ASP MVC Url参数

时间:2013-06-18 14:04:44

标签: asp.net-mvc-routing

我正在开发一个ASP.Net MVC项目。
我有一个特定的控制器动作,接受yyyy / mm / dd形式的日期值。因此URL变为

http://localhost/MyProject/PublicReview/GetReviews/2012/10/29.

其中GetReviews是一个动作,2012/10/29是参数。 我的RouteConfig如下:

     public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(name: "Default",url: "{controller}/{action}/{id}",defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
    }

我应该如何更改路线值? MapRoute值的顺序应该是什么?

1 个答案:

答案 0 :(得分:1)

我没有对此进行过测试,但我猜这会在您的方案中有效:

 routes.MapRoute( 
           "Reviews", "PublicReview/GetReviews/{year}/{month}/{day}" 
            { controller = "PublicReview", action = "GetReviews" };

请注意,这需要您的GetReviews方法有三个属性"年","月"和" day"。然后,您必须将它们解析为DateTime

取自http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-custom-routes-cs使用" - "用于日期分隔符。