【问题标题】:Using wildcards when mapping routes with MapWebPageRoute使用 MapWebPageRoute 映射路线时使用通配符
【发布时间】:2026-01-21 22:20:04
【问题描述】:

在一个 ASP.NET 网页项目(不是 Web 窗体,不是 MVC)中,我使用的是 Mike Brind 的 More Flexible Routing For ASP.NET Web Pages.

我想创建一个可以选择任意数量的路线元素但以特定单词结尾的路线,例如:

  • mydomain.com/route1/word
  • mydomain.com/route1/route2/word
  • mydomain.com/route1/route2/route3/word
  • ...等等等等

我在映射路线时尝试使用通配符,但不起作用,例如:

RouteTable.Routes.Ignore("{*routes}/word");

有没有办法映射这些路线可能性,或者我必须为每个可能性创建一条路线,例如:

  • RouteTable.Routes.MapWebPageRoute("{route1}/word", "~/mypage.cshtml");
  • RouteTable.Routes.MapWebPageRoute("{route1}/{route2}/word", "~/mypage.cshtml");
  • RouteTable.Routes.MapWebPageRoute("{route1}/{route2}/{route3}/word", "~/mypage.cshtml");
  • ...等等等等

【问题讨论】:

    标签: razor webmatrix asp.net-webpages


    【解决方案1】:

    我最终找到了解决办法。

    RouteTable.Routes.MapWebPageRoute("{*routes}",
        "~/mypage.cshtml",
        constraints: new { routes = @".*(/word)" });
    

    所以使用约束就是答案。

    【讨论】: