ASP.NET Core Web API 具有相同动作动词但不同签名的多个动作

ASP.NET Core Web API multiple actions with the same action verb but different signature

假设我有

[HttpPost]
public ActionResult<Object> Login([FromBody]LoginViewModel loginViewModel)
  {
    ....
  }

[HttpPost]
public ActionResult Logout()
  {
    ....
  }

在同一个控制器中。

And I am getting AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied:

...Login
...Logout

我可以简单地使用路由属性修复它,但我不明白为什么核心不绑定它。我的意思是签名不同。 ?

Web 的路由机制api,Core 和.NET framework 的工作方式相同。首先,他们会考虑控制器名称,然后他们会寻找合适的 http 方法,最后他们会查看查询字符串参数是否匹配(或路由指定的 url 中包含的参数)。因此,如果您的 2 个操作因取自 url 的参数而有所不同,那么就没有歧义。 body携带的参数不被路由机制解析,即使有或者有none。这源于一个事实,即 url 的参数只是普通字符串 - 易于比较。而 body 是 json 并且分析它更棘手。事实上,在登录方法的情况下,请求中没有参数 loginViewModel - 整个请求主体被序列化为 LoginViewModel 对象。