MVC 省略可选页面参数

发布于 2024-09-07 06:56:07 字数 583 浏览 3 评论 0原文

我想到了以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

分分钟 2024-09-14 06:56:07

我不确定我是否理解你想要什么,但不知何故我认为使用一些 正则表达式约束可能会解决您的问题。也许是这样的:

routes.MapRoute(null, "{category}/{branch}/{citywithp}{page}.html",
            new { controller = "Branch", action = "Index" },
            new {citywithp = @"p-\d+$" } );

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:

routes.MapRoute(null, "{category}/{branch}/{citywithp}{page}.html",
            new { controller = "Branch", action = "Index" },
            new {citywithp = @"p-\d+$" } );
一曲琵琶半遮面シ 2024-09-14 06:56:07

为了实现这一点,我需要 3 个路由:

routes.MapRoute(null, "{category}/{branch}/{city}.html",
                new { controller = "Branch", action = "Index" },
                new { page = "1" });

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 映射到第二个和第三个路由,将出站 URL 映射到第一个和第二个路由。

In order to achieve this i needed 3 routes:

routes.MapRoute(null, "{category}/{branch}/{city}.html",
                new { controller = "Branch", action = "Index" },
                new { page = "1" });

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 });

This way i can map all the urls inbound with the second and third route and outbound with the first and second.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文