asp.net mvc路由与id和slug之后的动作

时间:2013-06-21 22:26:20

标签: asp.net-mvc asp.net-mvc-routing

我在我的mvc web应用程序中添加了一条新路径,但它没有按预期工作。我希望有人可以帮我弄清楚它应该去哪里,也许应该为它定义哪些默认值和/或约束(在RouteConfig.cs中)。

所需的路线如下:

/controller/id/slug/action例如mydomain.com/products/10/product-name/reviews

我试图像这样定义这条路线,并尝试将其列为第一,第二和第三条路线:

routes.MapRoute(
   name: "AlternateRoute",
   url: "{controller}/{id}/{slug}/{action}",
   defaults: null,
   constraints: new { id = @"\d+", slug = @"[\w\-\d+]*" }
);

在我添加上述路线之后发生了什么,并浏览到/products/10/product-name之类的网页 - 之前类似于/ products / create的网址类似于/ products / 10 / product-name / create(但仅限于在那页上。)

我拥有的唯一其他路线是这3个(在我的routeConfig文件中定义):

/控制器/ ID /段塞

routes.MapRoute(
   name: "DefaultSlugRoute",
   url: "{controller}/{id}/{slug}",
   defaults: new { action = "Details", slug = "" },
   constraints: new { id = @"\d+", slug = @"[\w\-\d+]*" }
);

/控制器/动作/年/月

routes.MapRoute(
   name: "MonthlyArchiveRoute",
   url: "{controller}/{action}/{year}/{month}",
   defaults: new { controller = "Blog", action = "Archives", year = UrlParameter.Optional, month = UrlParameter.Optional },
   constraints: new { year = @"\d{4}", month = @"\d{2}" }
);

/ controller / action / id(包含w / new mvc项目的标准版)

routes.MapRoute(
   name: "DefaultRoute",
   url: "{controller}/{action}/{id}",
   defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

0 个答案:

没有答案