【问题标题】:The current request is ambiguous MVC Attribute Route当前请求不明确 MVC Attribute Route
【发布时间】:2017-07-05 02:02:06
【问题描述】:

有3个属性路由

[Route("{foo}_{bar}_{fee}_o_p")] ActionResult SelectFee
[Route("{foo}_{bar}_{fee}_{fii}_o_p")] ActionResult SelectFii
[Route("{foo}_{bar}_{fee}_{fii}_{fum}_o_p")] ActionResult SelectFum

前 2 个按预期工作。 但是最后一个出现了一个模棱两可的反射错误。

如果我将它们中的 2 个移到另一个区域/控制器也很奇怪,它们也会出现模棱两可的错误 如果我更新到 [Route("Select/{foo}_{bar}_{fee}_{fii}_{fum}_o_p")]ActionResult SelectFum 它有效...

路线是
public ActionResult SelectFee(string foo, string bar, string fee)
public ActionResult SelectFii(string foo, string bar, string fee, string fii)
public ActionResult SelectFum(string foo, string bar, string fee, string fii, string fum)

Elmah 的确切错误
System.Reflection.AmbiguousMatchException:当前请求在以下操作方法之间不明确:
RexProject.Controllers.ShopController
类型上的 System.Web.Mvc.ActionResult SelectedFee(System.String, System.String, System.String) RexProject.Controllers.ShopController
类型上的 System.Web.Mvc.ActionResult SelectFii(System.String, System.String, System.String, System.String) RexProject.Controllers.ShopController 类型上的 System.Web.Mvc.ActionResult SelectFum(System.String, System.String, System.String, System.String, System.String)

任何输入都会有所帮助! 谢谢!

解决方案(或解决方法)
去掉了Attribute路由,放到RouteConfig的RegisterRoutes Method里面,Longest First,按预期工作。

【问题讨论】:

  • 所有 3 条路由都是相同的 - 它们指定 url 仅包含一个段。
  • 显示您的完整操作签名。如果最后一个参数可以为空,那么这就是您收到该错误的原因

标签: asp.net-mvc asp.net-mvc-5 asp.net-mvc-routing


【解决方案1】:

如果这是标准获取,则不需要自定义路由,绑定将从查询字符串自动完成,看起来您只是想更改 url 格式,我不确定您从中获得了什么它。

public ActionResult SelectFum(string foo = null, string bar = null, string fee = null, string fii = null, string fum = null) 
{
   //your code
}

【讨论】:

  • 但这对 SEO 没有任何作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-12
  • 1970-01-01
  • 1970-01-01
  • 2013-07-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多