当操作名称以"Async&Quot;结尾时,ASP.NET Core 3.0 CreatedAtAction返回&Quot;没有与提供的值&Quot;匹配的路由 [英] Asp.Net Core 3.0 CreatedAtAction returns "no route matches the supplied values" when Action name ends with "Async"

查看:23
本文介绍了当操作名称以"Async&Quot;结尾时,ASP.NET Core 3.0 CreatedAtAction返回&Quot;没有与提供的值&Quot;匹配的路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在CreatedAtAction方面遇到了一个奇怪的问题,如果我的方法名以"async"关键字结尾,当我从Add方法返回CreatedAtAction时,我会得到一个500错误"No route matches the Provided Values"。如果我将GetRatePlanAs、GetRatePlanAsyncA或GetRatePlan等其他名称作为方法名,则它的效果非常不错。

如果我添加[ActionName("GetRatePlanAsync")]也可以,但我不想这样做。

CreatedAtAction:

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

不起作用:

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

作品:

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

同时工作:

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

推荐答案

经过几个小时的测试和工作,我找到了这些文章: https://github.com/aspnet/AspNetCore/issues/15316https://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

简而言之,这是ASP.NET Core 3.0中的一个突破性变化。

我真正喜欢的一个解决方案是设置选项。在配置服务启动中将SuppressAsyncSuffixInActionNames设置为false:

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

        ...
    }

这篇关于当操作名称以&quot;Async&Quot;结尾时,ASP.NET Core 3.0 CreatedAtAction返回&Quot;没有与提供的值&Quot;匹配的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆