找到多个与 URL 匹配的控制器类型.如果多个控制器上的属性路由与请求的 URL 匹配,就会发生这种情况 [英] Multiple controller types were found that match the URL. This can happen if attribute routes on multiple controllers match the requested URL

查看:14
本文介绍了找到多个与 URL 匹配的控制器类型.如果多个控制器上的属性路由与请求的 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)]

...您可以在 MVC 5.0 中使用相同的代码执行此操作,但 Order 参数除外.但是升级到MVC 5.1之后,你得到了问题标题中的异常信息:

... 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:

找到多个与 URL 匹配的控制器类型.这个可以如果多个控制器上的属性路由与请求的匹配,就会发生网址.

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 中你也可以做 SitePrecedence.当所有操作都在同一个控制器中时,是否只有像上面这样的路由?

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.

推荐答案

在属性路由的情况下,Web 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 会遵循路由优先级,您应该会看到您的场景正常工作.

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 匹配的控制器类型.如果多个控制器上的属性路由与请求的 URL 匹配,就会发生这种情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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