简体   繁体   中英

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(...)

After few hours of testing and stuff, I have found these articles: https://github.com/aspnet/AspNetCore/issues/15316 and https://docs.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);

        ...
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM