Search code examples
c#asp.netroutescustom-routes

Url Routing for unknown number of params


I have seen this post: MVC Handler for an unknown number of optional parameters but it's for MVC and doesn't seem to work for me as I get an error:

A path segment that contains more than one section, such as a literal section or a parameter, cannot contain a catch-all parameter.

I want to be able to have an indeterminate amount of params in a Url, I have the following route:

RouteCollection.MapPageRoute("ManyParam", "{*params}.html", "~/Default.aspx");

This also seems to trigger the error message above.

How can I set up a route to have an unknown number of parameters in web forms (not MVC).

I am trying to achieve the following urls:

www.example.com/some-thing.html
www.example.com/some-thing/else.html
www.example.com/and/some-thing/else.html
www.example.com/1/2/3/4/5/6.html

EDIT

It seems to work when I use the following:

RouteCollection.MapPageRoute("ManyParam", "{*params}", "~/Default.aspx");

The problem is with this is that it doesn't allow the .html at the end.


Solution

  • Untested route below - the wildcard one have to be absolutely last portion of Url. So to force ".html" at the end you need to use constraint (5th argument).

    routes.MapPageRoute(
        "ManyParam",
        "{*path}",
        "~/Default.aspx",
        false,
        new RouteValueDictionary(),
        new RouteValueDictionary { { "path", @".*\.html" } }
     );