Url Routing for unknown number of params
Asked Answered
T

1

0

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.

Tita answered 28/8, 2013 at 7:13 Comment(5)
Can you show how your desired Url look like? Your "{*params}.html" does not make it very clear what you want to achieve...Alodee
@AlexeiLevenkov - That would of been useful ... Added, thanks.Tita
Try "{*path}" for Url and have some sort of constraint that will require .html at the end using 5 arguments override of MapPageRouteAlodee
@AlexeiLevenkov - I have tried that: RouteCollection.MapPageRoute("ManyParam", "{*path}.html", "~/Default.aspx"); but I get the error: A path segment that contains more than one section, such as a literal section or a parameter, cannot contain a catch-all parameter. Am I doing something wrong with the .html at the end?Tita
I tried to add rough sample as answer...Alodee
A
1

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" } }
 );
Alodee answered 28/8, 2013 at 7:44 Comment(4)
Isn't that MVC though? I don't have a controller.Tita
@Tita - edited to probably be more correct - the point is you have to use constraint since you can't have anything past wildcard segment.Alodee
Right Ok, that falls in line with my tests I have been running. I will look up constraints with web forms and see what I can do. Thanks for your time.Tita
Works perfectly (with a slight tweak to the syntax). Many thanks again for your time.Tita

© 2022 - 2024 — McMap. All rights reserved.