Webforms 4 url routing multiple match

333 Views Asked by At

I have a webforms 4 project and I use url routing

In certain cases a given route would match more than one patterns.

It seems the routing mechanism tries each pattern in a certain order, and stops at the first match. (correct me if i am wrong). If the resource (mainly an aspx file) for that chosen pattern does not exist, we have a 404 error (resource does not exist).

The next matching pattern maps to an existing resource, but the mechanism does not try that.

Example (using a similar-to-MVC naming conventions)

routes.MapPageRoute("Action", "{controler}/{action}/{*pathInfo}", "~/Views/{controler}/{action}.aspx");
routes.MapPageRoute("Overview", "{controler}/{*pathInfo}", "~/Views/{controler}/Overview.aspx");

So there is a /Views/Patient/Overview.aspx and a /Views/Patient/Search.aspx on the file system.

The route "/Patient" will match the second pattern and map to "/Views/Patient/Overview.aspx"

The route "/Patient/Search" will match the first pattern and map to "/Views/Patient/Search.aspx"

The route "/Patient/Search/SomePathInfo" will match the first pattern and map to "/Views/Patient/Search.aspx" (treating"SomePathInfo" part of the url as the {*panthinfo} part)

Now, the problem is that the route "/Patient/SomePathInfo" matches both patterns. The first one treats "SomePathInfo" as the {action} part (searching for the "/Views/Patient/SomePathInfo.aspx" which does not exist ). The second one would treat "SomePathInfo" as the {*pathInfo} part and map to the existing "/Views/Patient/Overview.aspx".

The mechanism tries the first one though, it can't find a SomePathInfo.aspx file, and throws a 404 error.

My question is "Is there a way to direct the mechanism to try each pattern until it finds an existing resource? (or, more general, until some condition is satisfied? [here: resource.exists]"!

1

There are 1 best solutions below

0
Thanasis Ioannidis On

A workaround to my problem described above is to use routing constraints and define a limited set of available values for the {action} placeholder. The same applies to the {controller} placeholder etc.

So i define a constraint to accept only actions in the list: "index", "details", "add", "edit", "select" etc, and controllers in the list "home", "patient", "incident" etc;

routes.MapPageRoute("Action", "{controler}/{action}/{*queryValues}", "~/Views/{controler}/{action}.aspx", true,
            new RouteValueDictionary { // Default values
                { "controller", "home"},
                { "action", "index"} },
            new RouteValueDictionary { // constraints
                { "controller", "home|patient|incident"},
                { "action", "index|details|add|delete|edit|select"} });

That way, when I enter something like "patient/somequery", the "somequery" string does not satisfy the constraint of this mapping, so the mechanism continoues to the next one and treats the "somequery" part as the {*queryvalues} and not the {action}.

But the question still holds:

"Is there a way to direct the mechanism to try each pattern until it finds an existing resource? (or, more general, until some condition is satisfied? [here: resource.exists]"