swagger .net core API ambiguous HTTP Action debugging

可紊 提交于 2020-01-12 13:46:33

问题


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?


回答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 error before and usually the errormessage 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.




回答2:


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()
    };
}



回答3:


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




回答4:


Similar to totonho's answer you can use

[ApiExplorerSettings(IgnoreApi=true)]

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




回答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.



回答6:


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



来源:https://stackoverflow.com/questions/47822177/swagger-net-core-api-ambiguous-http-action-debugging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!