aspnet-api-versioning icon indicating copy to clipboard operation
aspnet-api-versioning copied to clipboard

Getting System.InvalidOperationException: No route matches the supplied values. while using CreatedAtAction

Open shardulshende opened this issue 6 years ago • 8 comments

[HttpPost]
        public async Task<IActionResult> PostAsync(RequestUserDto requestUserDto) {
            if (ModelState.IsValid && requestUserDto.RoleId != Guid.Empty) {
                 
                    MOM.Data.EntityModels.User user = _mapper.Map<MOM.Data.EntityModels.User>(requestUserDto);
                    await _userService.CreateAsync(user);
                    return CreatedAtAction("GetAsync", "User", new { Id = user?.Id }, "text");
            }
            return BadRequest(ModelState);
        }
[HttpGet("{id:Guid}", Name = nameof(GetAsync))]//Space in id:Guid(like "id : Guid" instead of id:Guid) results in error
        public async Task<IActionResult> GetAsync(Guid id) {
            if (ModelState.IsValid) {
                var user = await _userService.GetByIdAsync(id);
                if (user != null) {
                    return Ok(_mapper.Map<ResponsePostUserDto>(user));
                }
                return NoContent();
            }
            return BadRequest(ModelState);
        }

Above is my code. I am using framework .Net Core 3.1. In the above code request is executing successfully but showing error as below :

> System.InvalidOperationException: No route matches the supplied values.
>    at Microsoft.AspNetCore.Mvc.CreatedAtActionResult.OnFormatting(ActionContext context)
>    at Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor.ExecuteAsyncCore(ActionContext context, ObjectResult result, Type objectType, Object value)
>    at Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor.ExecuteAsync(ActionContext context, ObjectResult result)
>    at Microsoft.AspNetCore.Mvc.ObjectResult.ExecuteResultAsync(ActionContext context)
>    at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultAsync(IActionResult result)
>    at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
>    at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeNextResultFilterAsync[TFilter,TFilterAsync]()
> --- End of stack trace from previous location where exception was thrown ---
>    at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
>    at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
>    at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
> --- End of stack trace from previous location where exception was thrown ---
>    at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
>    at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
>    at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
>    at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
>    at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
>    at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
>    at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContextOfT`1.ProcessRequestAsync()

I have tried it in different ways as below :

return CreatedAtAction("GetAsync", "User", new { Id = user?.Id }, "text");
return CreatedAtAction("GetAsync", new { Id = user?.Id }, "text");
return CreatedAtAction(nameof(GetAsync), new { Id = user?.Id }, "text");
return CreatedAtAction(nameof(GetAsync), new { user?.Id }, "text");

Now I am not sure what I am doing wrong here.

shardulshende avatar Feb 01 '20 18:02 shardulshende

#558 I found this : Prefer CreatedAtAction over CreatedAtRoute as it does not have the naming restrictions. This obviously isn't an option for link generation outside of the current controller.

But it doesn't seem to work in my case.

If do version my Function name like GetAsyncV1 and use nameof(GetAsyncV1) then it's working properly. But I want my method name to be

GetAsync()

While using CreatedAtAction. How can I do it?

shardulshende avatar Feb 01 '20 18:02 shardulshende

Based on the information you've provided, this is almost certainly because of the Async suffix in your action names. I've seen people get hung up by this before. This is completely out of my hands and is a change in ASP.NET Core itself that addresses a separate bug. This behavior is called out as a breaking change in release notes here. There is apparently a way to enable the old behavior should you want to keep things they way they were.

I hope that helps.

commonsensesoftware avatar Feb 01 '20 20:02 commonsensesoftware

Thanks for the response. I was trying few scenarios on this and found that if ActionName attribute is used like this [ActionName("GetAsync")] it works properly. May be you can take this as input. Below is my working code :

[ActionName("GetAsync")]/*This required for CreatedAtAction in Post method otherwise has to version the API method*/
[HttpGet("GetAsync/{id}")]
public async Task<IActionResult> GetAsync(Guid id) {
   if (ModelState.IsValid) {
     var user = await _userService.GetByIdAsync(id);
     if (user != null) {
      return Ok(_mapper.Map<ResponsePostUserDto>(user));
      }
     return NoContent();
 }
 return BadRequest(ModelState);
 }
[HttpPost]
public async Task<IActionResult> PostAsync(RequestUserDto requestUserDto) {
 if (ModelState.IsValid && requestUserDto.RoleId != Guid.Empty) {
  MOM.Data.EntityModels.User user = _mapper.Map<MOM.Data.EntityModels.User>(requestUserDto);
   await _userService.CreateAsync(user);
  return CreatedAtAction(Common.HttpMethods.GetAsync.ToString(), new { user?.Id }, _mapper.Map<ResponsePostUserDto>(user));
 }
  return BadRequest(ModelState);
}

Below with Route attribute also works fine :

 [ActionName("GetAsync")]
        [Route("item/{id}")]
        [HttpGet("GetAsync")]
        public async Task<IActionResult> GetAsync(Guid id) {
            if (ModelState.IsValid) {
                var user = await _userService.GetByIdAsync(id);
                if (user != null) {
                    return Ok(_mapper.Map<ResponsePostUserDto>(user));
                }
                return NoContent();
            }
            return BadRequest(ModelState);
        }

Can you please verify whether this the expected behavior?

shardulshende avatar Feb 02 '20 09:02 shardulshende

One more thing if you can confirm [HttpGet("{id:int}", Name = nameof(GetAsync))] this code works fine [HttpGet("{id : int}", Name = nameof(GetTestByIdv1))] but this throws error(space in "id : int") Is this the expected behavior or it can be fixed to work with spaces?

shardulshende avatar Feb 02 '20 09:02 shardulshende

Your question is more general about how routing works in ASP.NET Core, which is better suited on Stackoverflow or the ASP.NET repo; however, your setup seems like should be fine. Route templates have their own markup and parsing rules. To my knowledge, white space is not allowed inside route constraints. I hope that helps.

It would seem that this issue has been resolved. Yes?

commonsensesoftware avatar Feb 15 '20 18:02 commonsensesoftware

This issue has been idle for quite some time. The original question appears to have been answered and resolved. If there are more question specific to API Versioning, I'm happy to answer them. Thanks.

commonsensesoftware avatar Sep 18 '20 22:09 commonsensesoftware

I tried to use strong typing and this worked for me, using .NET 5

[HttpGet("{id}")]
[ActionName(nameof(GetShipmentAsync))]
public async Task<ActionResult<Shipment>> GetShipmentAsync(int id) {
    var shipment = await _context.ShipmentsTable.FindAsync(id);

    return (ActionResult<Shipment>)shipment ?? NotFound();
}

[HttpPost]
public async Task<ActionResult<Shipment>> PostShipmentAsync([FromBody] Shipment shipment) {
    await _context.ShipmentsTable.AddAsync(shipment);
    await _context.SaveChangesAsync();

    return CreatedAtAction(nameof(GetShipmentAsync), new { id = shipment.Id }, shipment);
}

netotz avatar Jan 11 '21 22:01 netotz

Tried with same strong typing, it fixed this issue in .NET 8 [HttpGet("{stockId}")] [ActionName(nameof(GetByStockIdAsync))] public async Task GetByStockIdAsync([FromRoute] int stockId)

......

return CreatedAtAction(nameof(GetByStockIdAsync), new { stockId = comment.StockId }, comment.ToCommentDto());

nilanthasj avatar Aug 09 '24 14:08 nilanthasj