ASP.NET Core 3.0-CreatedAtRoute-没有路由与提供的值匹配

nop*_*nop 3 asp.net-core-3.0

从2.2更新到ASP.NET Core 3.0 No route matches the supplied values之后,在执行后立即出现CreateAsync。这是由CreatedAtAction引起的。我试图将GetByIdAsync的属性设置为,[HttpGet("{id}", Name = "Get")]但没有成功。我检查了其他相关线程,但是我的代码对我来说很好。

// GET: api/Bots/5
[HttpGet("{id}")]
public async Task<ActionResult<BotCreateUpdateDto>> GetByIdAsync([FromRoute] int id)
{
    var bot = await _botService.GetByIdAsync(id);

    if (bot == null)
    {
        return NotFound();
    }

    return Ok(_mapper.Map<BotCreateUpdateDto>(bot));
}

// POST: api/Bots
[HttpPost]
public async Task<ActionResult<BotCreateUpdateDto>> CreateAsync([FromBody] BotCreateUpdateDto botDto)
{
    var cryptoPair = await _botService.GetCryptoPairBySymbolAsync(botDto.Symbol);

    if (cryptoPair == null)
    {
        return BadRequest(new { Error = "Invalid crypto pair." });
    }

    var timeInterval = await _botService.GetTimeIntervalByIntervalAsync(botDto.Interval);

    if (timeInterval == null)
    {
        return BadRequest(new { Error = "Invalid time interval." });
    }

    var bot = new Bot
    {
        Name = botDto.Name,
        Status = botDto.Status,
        CryptoPairId = cryptoPair.Id,
        TimeIntervalId = timeInterval.Id
    };

    try
    {
        await _botService.CreateAsync(bot);
    }
    catch (Exception ex)
    {
        return BadRequest(new { Error = ex.InnerException.Message });
    }

    return CreatedAtAction(nameof(GetByIdAsync), new { id = bot.Id }, _mapper.Map<BotCreateUpdateDto>(bot));
}
Run Code Online (Sandbox Code Playgroud)

Ele*_*ron 12

我有同样的问题。我只更改endpoints.MapControllers();endpoints.MapDefaultControllerRoute();。第一个不指定任何路由,第二个设置默认路由。

app.UseEndpoints(endpoints =>
{
    endpoints.MapDefaultControllerRoute();
});
Run Code Online (Sandbox Code Playgroud)

  • 有没有关于他们为什么改变这一点的讨论? (2认同)