C
C#2y ago
Juicy

No route matches supplied values but values seem to match?

Hi I am getting this error on return CreatedAtAction:
System.InvalidOperationException: No route matches the supplied values.
System.InvalidOperationException: No route matches the supplied values.
var response = new UserResponse(
user.Id,
user.ProductId,
user.Username,
user.Role,
user.CreatedOnUtc);

var actionName = nameof(GetByIdAsync);
var routeValues = new {productId, id = user.Id, cancellationToken};

return CreatedAtAction(actionName, routeValues, response);
var response = new UserResponse(
user.Id,
user.ProductId,
user.Username,
user.Role,
user.CreatedOnUtc);

var actionName = nameof(GetByIdAsync);
var routeValues = new {productId, id = user.Id, cancellationToken};

return CreatedAtAction(actionName, routeValues, response);
[HttpGet("{id:guid}")]
public async Task<ActionResult> GetByIdAsync(Guid productId, Guid id,
CancellationToken cancellationToken = default)
{
[HttpGet("{id:guid}")]
public async Task<ActionResult> GetByIdAsync(Guid productId, Guid id,
CancellationToken cancellationToken = default)
{
But how are the route values incorrect when im giving it productId and the userId? To note, they also exist in the same controller and user.Id + productId are both guids (productId comes from parameter).Dont really understand what im missing here, let me know if you need me to provide more information
18 Replies
Angius
Angius2y ago
Could be because of the cancellation token? Never in my life have I seen a controller action that would take a cancellation token
Juicy
JuicyOP2y ago
oh I just assumed its used there lmao cause I was using it everywhere else ok I removed cancellation token but I still get the same error
Angius
Angius2y ago
Could you show the updated code?
Juicy
JuicyOP2y ago
Yep gimme a sec
[HttpPost("register")]
public async Task<ActionResult> RegisterAsync(Guid productId, [FromBody] CreateUserRequest request)
{
var user = await _userService.CreateAsync(
productId,
request.Username,
request.Password,
request.Role,
request.Hwid,
request.Ip,
request.LicenseKey);

var response = new UserResponse(
user.Id,
user.ProductId,
user.Username,
user.Role,
user.CreatedOnUtc);

var actionName = nameof(GetByIdAsync);
var routeValues = new {productId, id = user.Id};

return CreatedAtAction(actionName, routeValues, response);
}
[HttpPost("register")]
public async Task<ActionResult> RegisterAsync(Guid productId, [FromBody] CreateUserRequest request)
{
var user = await _userService.CreateAsync(
productId,
request.Username,
request.Password,
request.Role,
request.Hwid,
request.Ip,
request.LicenseKey);

var response = new UserResponse(
user.Id,
user.ProductId,
user.Username,
user.Role,
user.CreatedOnUtc);

var actionName = nameof(GetByIdAsync);
var routeValues = new {productId, id = user.Id};

return CreatedAtAction(actionName, routeValues, response);
}
[HttpGet("{id:guid}")]
public async Task<ActionResult> GetByIdAsync(Guid productId, Guid id)
{
var user = await _userService.GetByIdAsync(productId, id);

var response = new UserResponse(
user.Id,
user.ProductId,
user.Username,
user.Role,
user.CreatedOnUtc);

return Ok(response);
}

[HttpGet("{id:guid}")]
public async Task<ActionResult> GetByIdAsync(Guid productId, Guid id)
{
var user = await _userService.GetByIdAsync(productId, id);

var response = new UserResponse(
user.Id,
user.ProductId,
user.Username,
user.Role,
user.CreatedOnUtc);

return Ok(response);
}

Angius
Angius2y ago
I wonder if making the return type Task<IActionResult<UserResponse>> would change anything... I don't think it would, but might be worth a shot Everything else looks perfectly fine
Juicy
JuicyOP2y ago
Alright let me try I get this
Juicy
JuicyOP2y ago
Angius
Angius2y ago
Ah, so ActionResult<UserResponse>
Juicy
JuicyOP2y ago
oh whops
Angius
Angius2y ago
I can never remember if it's the interface or the actual type that's generic, my bad
Juicy
JuicyOP2y ago
Ok I get the same error its been driving me nuts because it seems fine as you said I even googled examples on how to use CreatedOnAction and tried following them 1:1 lol Could something in my service be affecting it..? even though ive debugged it and it does reach all the way down to CreatedOnAction...
Angius
Angius2y ago
Two more thoughts: 1. Try writing the action name manually instead of nameof(). Try "GetById", "getbyid", "getbyidasync", different combinations 2. In the route parameters object, try passing only one of them, either the productId or the id Trial and error is all I can come up with lol
Juicy
JuicyOP2y ago
Same issue the creation of the account goes through succesfully though
Angius
Angius2y ago
Weird
Juicy
JuicyOP2y ago
Oh Its because of Async suffix in my action name apparently thats not allowed when using createdataction...
Angius
Angius2y ago
Huh
Juicy
JuicyOP2y ago
GitHub
Getting System.InvalidOperationException: No route matches the supp...
[HttpPost] public async Task&lt;IActionResult&gt; PostAsync(RequestUserDto requestUserDto) { if (ModelState.IsValid &amp;&amp; requestUserDto.RoleId != Guid.Empty) { MOM.Data.Entity...
Juicy
JuicyOP2y ago
found this lol Yeah now it works wow
Want results from more Discord servers?
Add your server