1

I am trying to do API call-in.NET 5.0 through the post method but getting System.InvalidOperationException: No route matches the supplied values. can you please help me with the same?

My code

[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;
        }

    }
}
jps
  • 20,041
  • 15
  • 75
  • 79
Amol Naik
  • 433
  • 1
  • 8
  • 18

2 Answers2

2

I got this error when converting methods to be async. Updating the AddControllers function found in Startup.cs worked for me.

Solution here: ASP.NET CORE, Web API: No route matches the supplied values

Reason for issue: At runtime the async suffix is removed.

services.AddControllers(
    options => {
        options.SuppressAsyncSuffixInActionNames = false;
    }
);
Josef
  • 2,869
  • 2
  • 22
  • 23
Metcalfe
  • 61
  • 6
0

Have you tried using the RoutePrefix attribute at controller level? The Route attribute at Controller level is used as the fallback route for those actions within the controller that do not have a Route attribute set. In your case it seems that what you want is to actually use the “api/[controller]” as a prefix for the latter Routes defined at action level.

See more info about RoutePrefix and Route attribute in this link:

https://learn.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

Ed M.
  • 31
  • 1
  • 6