【问题标题】:The request matched multiple endpoints when migrated from .NET Core 2.1 to .NET 6从 .NET Core 2.1 迁移到 .NET 6 时请求匹配多个端点
【发布时间】:2023-02-03 11:17:57
【问题描述】:

这两个动作路由在 .netcore 2.1 中工作正常,但在迁移到 .netcore 6 后显示错误:AmbiguousMatchException: The request matched multiple endpoints 我关注了https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-6.0 但无法解决问题。

//Just for two route /content/game & /content/software  
[Route("/content/{pageId=game}")]
[Route("/content/{pageId=software}")]
public IActionResult Index1(string pageId, [FromQuery] int page=1)
{}

//For all other url(/content/*)
[Route("/content/{package}")]
public IActionResult Index2(string package)
{}

【问题讨论】:

  • 第二个有两条相同的路线?
  • Ralf 上面的评论,还有你的第一个动作路线以“/”开头,而第二个不是,是故意的吗?
  • 您实际上期望发生什么?我会让你的游戏/软件路线用他们的页面名称明确
  • 这是错字。更新问题。仍然显示错误。
  • 我想将/content/game/content/software路由到Index1/content/anythingElseIndex2

标签: c# asp.net-core


【解决方案1】:

You use regex to achieve it, Please refer:

//only match /context/game or /content/software

[Route("/content/{pageId:regex(^(game|software)$)}")]
[HttpGet]
public int Index1(string pageId, [FromQuery] int page = 1)
{
    return 2;
}

//For all other url(/content/*)      
[Route("/content/{package:regex(^(?!.*(game|software)).*$)}")]
[HttpGet]
public int Index2(string package)
{
   return 1;
}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 2020-08-03
    相关资源
    最近更新 更多