首页 > 解决方案 > System.InvalidOperationException:没有路由与提供的值匹配

问题描述

我正在尝试通过 post 方法执行 API call-in.NET 5.0,但得到 System.InvalidOperationException: No route 与提供的值匹配。你能帮我做同样的事情吗?

我的代码

[Route("api/[controller]")]
[ApiController]
public class CommonController : ControllerBase
{
    private readonly IConfiguration _configuration;
    private readonly ILanguages _Languages;
    public CommonController(ILanguages Languages, IConfiguration configuration)
    {
        _Languages = Languages;
        _configuration = configuration;
    }


    [HttpGet, Route("GetLanguages")]
    public Languages GetLanguages()
    {
        try
        {
            return _Languages.GetLanguages();

        }
        catch (Exception)
        {

            throw;
        }

    }
}

标签: c#api.net-core.net-5

解决方案


将方法转换为异步时出现此错误。更新 Startup.cs 中的 AddControllers 函数对我有用。

此处的解决方案:ASP.NET CORE,Web API:没有路由与提供的值匹配

问题原因:在运行时,异步后缀被删除。

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

推荐阅读