gpt4 book ai didi

asp.net-core - ASP.NET 核心 3.0 : CreatedAtRoute: No route matches the supplied values

转载 作者:行者123 更新时间:2023-12-04 08:29:19 33 4
gpt4 key购买 nike

从 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));
}

最佳答案

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

app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();
});

关于asp.net-core - ASP.NET 核心 3.0 : CreatedAtRoute: No route matches the supplied values,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58183972/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com