2

Steps to reproduce:

  • Create a new Web API project
  • Create a UsersController with the following code

.

[ApiController]
[Route("[controller]")]
public class UsersController : ControllerBase
{
    [HttpGet("{id:int}", Name = nameof(GetUserByIdAsync))]
    public async Task<ActionResult<object>> GetUserByIdAsync([FromRoute] int id)
    {
        object user = null;

        return Ok(user);
    }

    [HttpPost]
    public async Task<ActionResult<object>> CreateUserAsync()
    {
        object user = null;

        return CreatedAtAction(nameof(GetUserByIdAsync), new { id = 1 }, user);
    }
}

System.InvalidOperationException: No route matches the supplied values.

  • Rename both methods by removing the Async from the method names, the methods should look like

    [HttpGet("{id:int}", Name = nameof(GetUserById))]
    public async Task<ActionResult<object>> GetUserById([FromRoute] int id)
    {
        // ...
    }
    
    [HttpPost]
    public async Task<ActionResult<object>> CreateUser()
    {
        // ...
    }
    
  • Call the url POST https://localhost:5001/users again

  • You will receive an empty 201 response

So I'm assuming the error occurs with the method names, any ideas?

1 Answer 1

3

System.InvalidOperationException: No route matches the supplied values.

To fix above error, you can try to set SuppressAsyncSuffixInActionNames option to false, like below.

services.AddControllers(opt => { 
    opt.SuppressAsyncSuffixInActionNames = false; 
});

Or apply the [ActionName] attribute to preserve the original name.

[HttpGet("{id:int}", Name = nameof(GetUserByIdAsync))]
[ActionName("GetUserByIdAsync")]
public async Task<ActionResult<object>> GetUserByIdAsync([FromRoute] int id)
{
Sign up to request clarification or add additional context in comments.

3 Comments

I would rather go for this opt.SuppressAsyncSuffixInActionNames = false; but what is the reason? And what are the disadvantages?
It would be nice if you knew something about loss of features when setting the global bool
You can check this doc to know more about this change: learn.microsoft.com/en-us/dotnet/core/compatibility/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.