Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core 3.0: CreatedAtRoute: No route matches the supplied values

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));
}
like image 842
nop Avatar asked Oct 01 '19 11:10

nop


1 Answers

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();
});
like image 80
Electron Avatar answered Jan 03 '23 17:01

Electron