带有可选部分的 MVC 路由
我想为我的 MVC 页面创建一条如下所示的路线:
/Articles/
/文章/页/2
/Articles/Page/3
我希望默认页面为 1,但如果页面为 1,则实际上不显示 /Page/ 部分。
我开始于:
routes.MapRoute(
"Articles",
"Articles/Page/{page}",
new { controller = "Articles", action = "Index", page = 1 }
);
问题在于,当我这样做时:
<%= Html.RouteLink("Articles", new { page = 1 }) %>
我的路线最终是: /Articles/Page/
I would like to create a route for my MVC page that looks like this:
/Articles/
/Articles/Page/2
/Articles/Page/3
I want the default page to be 1, but if the page is 1, then don't actually show the /Page/ piece.
I started out with:
routes.MapRoute(
"Articles",
"Articles/Page/{page}",
new { controller = "Articles", action = "Index", page = 1 }
);
The problem with this is that when I do:
<%= Html.RouteLink("Articles", new { page = 1 }) %>
My route ends up being: /Articles/Page/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能需要两个路由定义(未经测试):
以及您的控制器操作:
You might need two route definitions for this (untested):
and your controller action:
将两者都放入:
您不需要对控制器执行任何操作。
Put both in:
You don't need to do anything with the controller.