Reputation: 6359
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));
}
Upvotes: 5
Views: 2360
Reputation: 137
Besides .MapDefaultControllerRoute() above I had to structure two endpoints as follows based upon (and extrapolated from) the URLs mentioned in the comments below.
[HttpPost]
[Route( "CreateServedByMedicalItem" )]
public IActionResult HTTPpost( [FromBody] ServedByMedicalDTO aServedByMedicalCreateDto )
{
string userName = "userToDo";
ServedByMedical aServedByMedicalItem;
try
{
if (aServedByMedicalCreateDto == null)
{
throw new Exception( "Input aServedByMedicalCreateDto did not translate into a ServedByMedicalDTO." );
}
aServedByMedicalItem = EMX2.ToInsertServedByMedical( aServedByMedicalCreateDto, userName, _mapper );
int MedicalItemId = _repo.EFadd( aServedByMedicalItem );
// this is only puts commands in the queue, but with new/next id ???
if (MedicalItemId == 0
&& aServedByMedicalItem.ExceptionInfo != null
&& aServedByMedicalItem.ExceptionInfo.Length > 0)
{
throw new Exception( aServedByMedicalItem.ExceptionInfo );
}
var Id = aServedByMedicalItem.IdForComplaint;
return CreatedAtRoute(
routeName: "MethGetOneServedByMedical",
routeValues:new
{
UniqueIdOfOneComplaint = Id
},
value: aServedByMedicalItem
); // HTTP 201
}
catch (Exception ex)
{
string msg = ex.Message;
if (ex.InnerException != null)
{
msg = msg + " | " + ex.InnerException.Message.ToString();
}
return BadRequest( msg );
}
// REST standard is to have the URI of the new row passed back in HEADER
// (location)
}
[HttpGet( "GetOneServedByMedical/{UniqueIdOfOneComplaint:int}", Name = nameof( MethGetOneServedByMedical ))] // https://github.com/microsoft/aspnet-api-versioning/issues/558
[ActionName( "GetOneServedByMedical" )]
// https://www.josephguadagno.net/2020/07/01/no-route-matches-the-supplied-values
// however, I had to hard code "GetOneServedByMedical" to be the endpoint name
// recognized by CreatedAtRoute in HTTPpost
public IActionResult MethGetOneServedByMedical( int UniqueIdOfOneComplaint )
{
// Instantiate source object
// (Get it from the database or whatever your code calls for)
var obj = _repo.GetServedByMedicalGivenComplaintId( UniqueIdOfOneComplaint );
ServedByMedicalViewModel _view = _mapper.Map<ServedByMedicalViewModel>( obj );
return Ok(_view);
}
Upvotes: 0
Reputation: 1445
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();
});
Upvotes: 15