【发布时间】:2009-06-03 12:24:10
【问题描述】:
我正在尝试启用如下路线
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”工作正常)。有人知道我在做什么错吗?提前谢谢你。
编辑:
我为这个问题找到了一个简单的解决方法:
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 是“-”,我们匹配第一个路由,否则匹配第二个。
【问题讨论】: