0

我有一个 webforms 4 项目,我使用 url 路由

在某些情况下,给定的路由会匹配多个模式。

似乎路由机制以特定顺序尝试每个模式,并在第一个匹配时停止。(如果我错了,请纠正我)。如果所选模式的资源(主要是 aspx 文件)不存在,则会出现 404 错误(资源不存在)。

下一个匹配模式映射到现有资源,但该机制不会尝试这样做。

示例(使用类似于 MVC 的命名约定)

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

所以文件系统上有一个/Views/Patient/Overview.aspx 和一个/Views/Patient/Search.aspx。

路由“ /Patient ”将匹配第二个模式并映射到“ /Views/Patient/Overview.aspx

路由“ /Patient/Search ”将匹配第一个模式并映射到“ /Views/Patient/Search.aspx

路由“ /Patient/Search/SomePathInfo ”将匹配第一个模式并映射到“ /Views/Patient/Search.aspx ”(将 url 的“SomePathInfo”部分视为 {*panthinfo} 部分)

现在,问题是路线“ /Patient/SomePathInfo ”匹配这两种模式。第一个将“SomePathInfo”视为 {action} 部分(搜索不存在的“ /Views/Patient/SomePathInfo.aspx ”)。第二个将“SomePathInfo”视为 {*pathInfo} 部分并映射到现有的“ /Views/Patient/Overview.aspx ”。

虽然该机制尝试第一个,但它找不到 SomePathInfo.aspx 文件,并引发 404 错误。

我的问题是“有没有办法指导机制尝试每种模式,直到找到现有资源? (或者,更一般地说,直到满足某些条件?[这里:resource.exists] ”!

4

1 回答 1

0

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]"

于 2012-08-09T10:21:56.353 回答