多个控制器类型中发现匹配的网址。如果在多台控制器属性的路由匹配请求的URL会发生这种情况 [英] Multiple controller types were found that match the URL. This can happen if attribute routes on multiple controllers match the requested URL

查看:544
本文介绍了多个控制器类型中发现匹配的网址。如果在多台控制器属性的路由匹配请求的URL会发生这种情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

...猜我是第一个要问这个呢?

...guess I'm the first to ask about this one?

假设你有以下途径,每一个不同的控制器上宣布:

Say you have the following routes, each declared on a different controller:

[HttpGet, Route("sign-up/register", Order = 1)]
[HttpGet, Route("sign-up/{ticket}", Order = 2)]

...你可以用相同的code除外订单参数为此在MVC 5.0。但是,在升级到5.1 MVC后,您在问题的标题获得异常消息:

... you could do this in MVC 5.0 with the same code except for the Order parameter. But after upgrading to MVC 5.1, you get the exception message in the question title:

多控制器类型被发现匹配网址。这可以
  发生,如果在多台控制器属性的路由符合要求
  网址。

Multiple controller types were found that match the URL. This can happen if attribute routes on multiple controllers match the requested URL.

因此​​,新的 RouteAttribute.Order 属性仅控制器级?我知道AttributeRouting.NET你可以做网​​站precedence 太。是有像上面的路由时,所有操作都在同一个控制器的唯一途径?

So the new RouteAttribute.Order property is only controller-level? I know in AttributeRouting.NET you can do SitePrecedence too. Is the only way to have routes like the above when all actions are in the same controller?

更新

对不起,我应该提到航线上MVC控制器,不是的WebAPI。我不知道这将如何影响ApiControllers。

Sorry, I should have mentioned these routes are on MVC controllers, not WebAPI. I am not sure how this affects ApiControllers.

推荐答案

在属性路由的情况下,网络API试图找到所有匹配的请求该控制器。如果它看到多个控制器能够处理这一点,那么它,因为它认为这是一个可能的用户错误抛出异常。这条路线探测距离常规路由,其中​​第一场比赛获胜的不同。

In case of Attribute routing, Web API tries to find all the controllers which match a request. If it sees that multiple controllers are able to handle this, then it throws an exception as it considers this to be possibly an user error. This route probing is different from regular routing where the first match wins.

作为一种解决方法,如果你有相同的控制器内这两个动作,则Web API荣誉路线precedence,你应该看到您的方案的工作。

As a workaround, if you have these two actions within the same controller, then Web API honors the route precedence and you should see your scenario working.

这篇关于多个控制器类型中发现匹配的网址。如果在多台控制器属性的路由匹配请求的URL会发生这种情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆