Url Routing for unknown number of params

China☆狼群 提交于 2020-01-06 14:07:35

问题


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.


回答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" } }
 );


来源:https://stackoverflow.com/questions/18481484/url-routing-for-unknown-number-of-params

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!