MVC 省略可选页面参数
我想到了以下 URL:
/restaurants/italian/miami.html
/restaurants/italian/miami-p2.html
使用这些路由
routes.MapRoute(null, "{category}/{branch}/{city}-p{page}.html",
new { controller = "Branch", action = "Index" });
routes.MapRoute(null, "{category}/{branch}/{city}.html",
new { controller = "Branch", action = "Index", page = 1 });
现在,对于我的问题,我希望将 url 的“-p{page}”部分设置为可选,而不仅仅是 {page} 参数。这样我就可以使用单个路由,并使用它来映射带有 Url.RouteUrl(RouteValueDictionary)
的出站 URL(如果字典中的页面参数为 1,则自动删除页面部分)。
I have the following URL in mind:
/restaurants/italian/miami.html
/restaurants/italian/miami-p2.html
Using these routes
routes.MapRoute(null, "{category}/{branch}/{city}-p{page}.html",
new { controller = "Branch", action = "Index" });
routes.MapRoute(null, "{category}/{branch}/{city}.html",
new { controller = "Branch", action = "Index", page = 1 });
Now for my question, i want to make "-p{page}" portion of the url optional, not just the {page} parameter. That way i can use a single route and also use it to map outbound urls with Url.RouteUrl(RouteValueDictionary)
(which then auto removes the page portion if the page parameter in the dictionary is 1).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定我是否理解你想要什么,但不知何故我认为使用一些 正则表达式约束可能会解决您的问题。也许是这样的:
I am not sure I understand well what you would like, still somehow I think that using some regular expression constraint might solve your problem. Maybe somehow like this:
为了实现这一点,我需要 3 个路由:
这样我可以将所有入站 URL 映射到第二个和第三个路由,将出站 URL 映射到第一个和第二个路由。
In order to achieve this i needed 3 routes:
This way i can map all the urls inbound with the second and third route and outbound with the first and second.