Asp.net mvc 4 mvc MapRoute:如何生成带有段(斜线)和变量(在后面传递)的路径

Asp.net mvc 4 mvc MapRoute:如何生成带有段(斜线)和变量(在后面传递)的路径,asp.net-mvc-4,Asp.net Mvc 4,我需要生成以下路径 /products/tires/all/all/all-all-Rall?page=2&ItemsPerPage=50 我要做的是解决这个问题: routes.MapRoute("Tyres", "products/tyres/{ProducerId}/{SeasonId}/{Width}-{Height}-R{Diametr}", new { controller = "Products", action = "Tyres", ProducerId =

我需要生成以下路径 /products/tires/all/all/all-all-Rall?page=2&ItemsPerPage=50 我要做的是解决这个问题:

   routes.MapRoute("Tyres", "products/tyres/{ProducerId}/{SeasonId}/{Width}-{Height}-R{Diametr}",
        new { controller = "Products", action = "Tyres", ProducerId = "all", SeasonId = "all", Width = "all", Height = "all", Diametr = "all", page = UrlParameter.Optional, ItemsPerPage =  UrlParameter.Optional });
当我写
@Url.Action(“轮胎”,“产品”,新的{ProducerId=“5”,Height=“55”})
时,路线是正确的/Products/tires/5/all/all-55-Rall

但是当我添加页面参数:
@Url.Action(“轮胎”,“产品”,新的{ProducerId=“5”,Height=“55”,page=2})
时,生成的路线没有分段:/Products/tires?ProducerId=5&Height=55&page=10


如何解决这个问题

如果我正确理解您的问题,并且您希望看到url格式为:

 /products/tyres/5/all/all-55-Rall?page=2&ItemsPerPage=6
,然后通过从发布的模板中排除可选参数来尝试路由模板:

        routes.MapRoute("Tyres", "products/tyres/{ProducerId}/{SeasonId}/{Width}-{Height}-R{Diametr}",
                               new
                               {
                                   controller = "products",
                                   action = "Tyres",
                                   ProducerId = "all",
                                   SeasonId = "all",
                                   Width = "all",
                                   Height = "all",
                                   Diametr = "all"
//Notice here, i have excluded the Optional URL parameters from your template: 
//page = UrlParameter.Optional, ItemsPerPage =  UrlParameter.Optional 
                               } 
                           );
如果指定了可选参数,并且调用时未提供这些参数,则URL将无效:

/products/tyres/5/all/all-55-Rall//ItemsPerPage=6 //Notice here page value is not provided
,因此它可能使用querystring方法来处理此问题


希望这对您有所帮助。

您是否尝试通过定义所有需要的额外参数来定义可以处理此特定请求的额外路由?还要确保所需的额外参数用“/”而不是“-”分隔。我对“-”没有问题。你明白我的问题吗?我需要生成一条包含分段的路线:/产品/轮胎。。。参数:page=2&ItemsPerPage=50。我不知道怎么做。请写出具体的例子。