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

编程入门 行业动态 更新时间:2024-10-17 07:26:02
本文介绍了找到多个与 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 匹配,就会发生这种情况

本文发布于:2023-11-02 17:03:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552937.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   就会   这种情况   路由   控制器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!