本文介绍了ASP.NET路由和普通防爆pressions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使像一个路线以下

I am trying to enable a route like the following

route = new Route("{w1}-{c1}-{n1},{w2}-{c2}-{n2}", new ResultRouteHandler());
route.Constraints = new RouteValueDictionary();
route.Constraints.Add("c1", "(.*)|([-])");
route.Constraints.Add("c2", "(.*)|([-])");
RouteTable.Routes.Add(route);

不过,我碰到一个问题,当C1或C2为 - 。例如,A-B-C,D --- F返回404(同时A-B-C,D-E-F正常工作)。任何人有一个线索我究竟做错了什么?谢谢你在前进。

However I run into a problem when c1 or c2 is "-". For example "a-b-c,d---f" returns 404 (whilst "a-b-c,d-e-f" works fine). Anyone has a clue what am I doing wrong? Thank you in advance.

编辑:

我发现了一个简单的办法解决这个问题:

I found a simple workaround for this problem:

route = new Route("{w1}-{c1}-{n1},{w2}---{n2}", new MyRouteHandler());
RouteTable.Routes.Add(route);
route = new Route("{w1}-{c1}-{n1},{w2}-{c2}-{n2}", new MyRouteHandler());
RouteTable.Routes.Add(route);

如果C2为 - 。我们匹配到第一路径,否则到第二

If c2 is "-" we match to the first route, otherwise to the second.

推荐答案

如果我理解正确的话,您要匹配ABC,D ---˚F 这就是为什么你会在第一时间调整的约束。你有没有那种多余的,虽然为正则表达式 - 将由匹配'*'。换句话说,我不认为你的正则表达式是难辞其咎的,而是路由引擎解析器。

If I'm understanding correctly, you do want to match "a-b-c,d---f" which is why you're tweaking the constraints in the first place. The Regex you have there is kind of redundant though as '-' will be matched by the '.*'. In other words, I don't think your Regex is to blame but rather the routing engine parser.

如果您从{W1​​} - {C1} - {N1},{W2} - {C2} - {N2}改变你的路线{W1​​} - {C1} - {N1},{W2} _ {C2} - {N2},那么C2将开始匹配 - ABC,D _-- F。我认为,一些有关路线的分析不喜欢用分隔符作为下一个值。

If you change your route from "{w1}-{c1}-{n1},{w2}-{c2}-{n2}" to "{w1}-{c1}-{n1},{w2}_{c2}-{n2}", then c2 will start matching the '-' in "a-b-c,d_--f". I think something about the parsing of routes doesn't like using a delimiter as the next value.

所以,我认为你可以将你的约束(因为它们目前存在的),但你可能需要组织您的网址有点不同,如果你想 - C1 C2

So I think you can drop your constraints (as they currently exist), but you might need to organize your URLs a little differently if you want '-' to be c1 or c2.

这篇关于ASP.NET路由和普通防爆pressions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 23:05