Using wildcards when mapping routes with MapWebPageRoute

469 views Asked by At

In an ASP.NET Web Pages project (not Web Forms, not MVC) I am using Mike Brind's More Flexible Routing For ASP.NET Web Pages.

I want to create a route that picks ups any number of route elements but ending in a specific word, eg:

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

I tried to use wildcards when mapping the route but that didn't work, eg:

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

Is there a way to map these route possibilites or do I have to create a route for each possibiility, eg:

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

There are 1 answers

0
johna On BEST ANSWER

I eventually figured out a solution to this.

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

So using constraints was the answer.