After updating to ASP.NET Core 3.0 from 2.2, I am receiving the error:
No route matches the supplied values
This appears right after executing CreateAsync()
. It is triggered by the CreatedAtAction()
method. I tried to set GetByIdAsync()
's attribute to [HttpGet("{id}", Name = "Get")]
, but it didn't work out. I checked other related threads but my code looks fine to me.
// 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));
}
I had same issue. I only changed endpoints.MapControllers();
to endpoints.MapDefaultControllerRoute();
. The first one doesn't specify any routes and the second one sets the default route.
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With