Asp.net mvc MVC省略可选页面参数

Asp.net mvc MVC省略可选页面参数,asp.net-mvc,asp.net-mvc-2,asp.net-mvc-routing,Asp.net Mvc,Asp.net Mvc 2,Asp.net Mvc Routing,我心目中有以下网址: /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}

我心目中有以下网址:

/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映射到
Url.RouteUrl(RouteValueDictionary)
(如果字典中的页面参数为1,它会自动删除页面部分)。

我不确定我是否很理解您想要的内容,但我认为使用一些可能会解决您的问题。也许是这样的:

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

我不确定我是否很了解你想要什么,但不知何故我认为使用一些可能会解决你的问题。也许是这样的:

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

为了实现这一点,我需要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映射到第一条和第二条路由。

为了实现这一点,我需要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映射到第一个和第二个路由。

为什么要将
.html
添加到路由中?路由的全部意义在于不指向物理页面。我的想法完全正确,但不幸的是,这不是我的决定。为什么要将
.html
添加到路由中?路由的全部意义在于不指向物理页面。我的想法是正确的,但这不是我的决定。不幸的是,我很高兴它在某种程度上帮助了我……)好吧,我很高兴它在某种程度上帮助了我……:)