在MVC中为某些类型的URL自定义路由

时间:2014-07-09 13:23:39

标签: asp.net-mvc

我需要重定向类型为

的网址
www.mypage.com/news/1.1.2014
www.mypage.com/news/2.2.2014
www.mypage.com/news/3.3.2014

到名为"新闻"的控制器和行动叫"索引"。日期可以是任何参数。

这是正确的方法吗? (不能测试它是否有效,因为我没有MVC应用程序来测试它)

name: "News",
url: {controller}/{action}/{date},
defaults: new {controller="News", action="Index" , date=UrlParameter.Optional}

name: "News",
url: News/{action}/{date},
defaults: new {controller="News", action="Index" , date=UrlParameter.Optional}

这些都是正确的方法吗?如果没有,请帮助我。谢谢。

1 个答案:

答案 0 :(得分:0)

您的路线需要注册如下:

name: "News",
url: "news/{date}",
defaults: new { controller = "News", action = "Index", date = UrlParameter.Optional }

请注意,路线必须在任何其他路线之前注册,而不能匹配网址模式(包括默认/后备路由)。