Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net Core 3.0 CreatedAtAction returns "no route matches the supplied values" when Action name ends with "Async"

I had a strange problem with CreatedAtAction, if my method name ends with "Async" keyword, I get an 500 error "no route matches the supplied values" when I return CreatedAtAction from my Add method. If I put anything else as method name like GetRatePlanAs, GetRatePlanAsyncA or GetRatePlan then it works like a charm.

It would also work if I add [ActionName("GetRatePlanAsync")] but I didn't want to do that.

CreatedAtAction:

return CreatedAtAction(nameof(GetRatePlanAsync), new { ... }, null);

Doesn't work:

    [HttpGet]
    [Route("item")]
    public async Task<ActionResult> GetRatePlanAsync(...)

Works:

    [HttpGet]
    [Route("item")]
    [ActionName("GetRatePlanAsync")]
    public async Task<ActionResult> GetRatePlanAsync(...)

Also work:

    [HttpGet]
    [Route("item")]
    public async Task<ActionResult> GetRatePlan(...)
like image 513
Nemanja Todorovic Avatar asked Dec 11 '19 14:12

Nemanja Todorovic


1 Answers

After few hours of testing and stuff, I have found these articles: https://github.com/aspnet/AspNetCore/issues/15316 and https://learn.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.0&tabs=visual-studio#async-suffix-removal-from-controller-action-names

In short, that is a breaking change in Asp.Net Core 3.0.

One solution for this that I actually liked was to set options.SuppressAsyncSuffixInActionNames to false in Configure Services Startup:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers(options => options.SuppressAsyncSuffixInActionNames = false);

        ...
    }
like image 124
Nemanja Todorovic Avatar answered Oct 11 '22 14:10

Nemanja Todorovic