'swagger .net core API ambiguous HTTP method for Action Error

Implementing Swashbuckle/Swagger with .net Core 2 API I am now receiving the 500 error when accessing swagger.json:

NotSupportedException: Ambiguous HTTP method for action - EBisAPI.Controllers._class.HandleError (EBisAPI). Actions require an explicit HttpMethod binding for Swagger

I have gone through all the controllers and see explicit routing on all the public methods of each controller. Is there a way to determine which method is throwing the ambiguous routing error?



Solution 1:[1]

This can occur when a method is declared public in a controller, but without REST attributes. Changing the method to protected may address the issue.

I have seen this kind of error before and usually the error message points to the culprit: EBisAPI.Controllers._class.HandleError

I guess HandleError is a public method in your base class, right? Change it to protected and try again.

This is of course only one possible solution. If the method which is mentioned in the error message is part of an interface implementation, it doesn't work and you need to look at one of the other solutions.

Solution 2:[2]

Similar to totonho's answer you can use

[ApiExplorerSettings(IgnoreApi=true)]

(From https://github.com/domaindrivendev/Swashbuckle/issues/153 )

Solution 3:[3]

The Clean way could be using data annotation [NonAction] instead to set your method as protected.

Solution 4:[4]

I solved this error by decorating the method mentioned in the error with the correct HTTP attribute. For exemple: [HttpGet]

This code throws error:

public object Get()
{
    MongoDbContext dbContext = new MongoDbContext();
    return new {
        API = true,
        Database = dbContext.Status()
    };
}

This code works:

[HttpGet]
public object Get()
{
    MongoDbContext dbContext = new MongoDbContext();
    return new {
        API = true,
        Database = dbContext.Status()
    };
}

Solution 5:[5]

As i mentioned at "Swashbuckle not creating swagger.json file" :

  1. Decorate all actions with explicit Http Methods like[HttpGet("xxx")],[HttpPost("xxx")] or ... instead of [Route("xxx")].
  2. Decorate public methods in controllers with [NoAction] Attribute.

Solution 6:[6]

I tried different solutions but what it worked for me was adding the route inside the attributes as stated above. I.E: [HttpPost("yourRoute/create")] one of the issues was that I have 2 get methods so I changed their names and added the route to the attributes as I said.

Solution 7:[7]

I Had the same problem, and was because i have done the override of the Ok() method ,returning an OkObjectResult. The solution was change it from public to protected

Solution 8:[8]

I was facing the same issue. For me the mistake was by default WeatherForecastController or any other class was inheriting from ControllerBase class. But when I created a new one, it inherited from Controller class.

So this conflicts between ControllerBase and Controller class was causing the issue.

Then inheriting the new Controller from ControllerBase class fixed the issue for me.

You can't inherit from both ControllerBase and Controller at the same time.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Roman Marusyk
Solution 3 totonho
Solution 4 Guilherme Ferreira
Solution 5 Mohammad Barbast
Solution 6 Dharman
Solution 7
Solution 8 Shakibuz_Zaman