swagger .net core API 动作错误的不明确 HTTP 方法
swagger .net core API ambiguous HTTP method for Action Error
使用 .net Core 2 实现 Swashbuckle/Swagger API 我现在在访问 swagger.json 时收到 500 错误:
NotSupportedException: Ambiguous HTTP method for action -
EBisAPI.Controllers._class.HandleError (EBisAPI). Actions require an
explicit HttpMethod binding for Swagger
我遍历了所有控制器,并在每个控制器的所有 public 方法上看到了显式路由。有没有办法确定哪个方法引发了不明确的路由错误?
当在控制器中声明方法 public 但没有 REST 属性时,可能会发生这种情况。将方法更改为受保护可能会解决问题。
我以前见过这种错误,通常错误消息指向罪魁祸首:
EBisAPI.Controllers._class.HandleError
我猜 HandleError
是你的基础 class 中的一个 public
方法,对吧?将其更改为 protected
并重试。
这当然只是一种可能的解决方案。如果错误消息中提到的方法是接口实现的一部分,则它不起作用,您需要查看其他解决方案之一。
Clean 方法可以使用数据注释 [NonAction]
而不是将您的方法设置为受保护。
类似于你可以使用
[ApiExplorerSettings(IgnoreApi=true)]
(来自 https://github.com/domaindrivendev/Swashbuckle/issues/153 )
我通过用正确的 HTTP 属性修饰错误中提到的方法解决了这个错误。例如:[HttpGet]
此代码抛出错误:
public object Get()
{
MongoDbContext dbContext = new MongoDbContext();
return new {
API = true,
Database = dbContext.Status()
};
}
此代码有效:
[HttpGet]
public object Get()
{
MongoDbContext dbContext = new MongoDbContext();
return new {
API = true,
Database = dbContext.Status()
};
}
正如我在 中提到的:
- 用
[HttpGet("xxx")]
、[HttpPost("xxx")]
或...等显式 Http 方法装饰所有操作,而不是 [Route("xxx")]
。
- 用
[NoAction]
属性修饰控制器中的 public 方法。
我遇到了同样的问题,是因为我重写了 Ok() 方法,返回了一个 OkObjectResult。解决方案是将其从 public 更改为 protected
我尝试了不同的解决方案,但对我有用的是如上所述在属性中添加路由。 I.E:[HttpPost("yourRoute/create")]
其中一个问题是我有 2 个 get 方法,所以我更改了它们的名称并按照我所说的将路由添加到属性。
我遇到了同样的问题。对我来说,错误是默认 WeatherForecastController
或任何其他 class 继承自 ControllerBase
class。但是当我创建一个新的时,它继承自 Controller
class.
所以 ControllerBase
和 Controller
class 之间的冲突导致了这个问题。
然后从 ControllerBase
继承新的控制器 class 为我解决了这个问题。
您不能同时继承 ControllerBase
和 Controller
。
使用 .net Core 2 实现 Swashbuckle/Swagger API 我现在在访问 swagger.json 时收到 500 错误:
NotSupportedException: Ambiguous HTTP method for action - EBisAPI.Controllers._class.HandleError (EBisAPI). Actions require an explicit HttpMethod binding for Swagger
我遍历了所有控制器,并在每个控制器的所有 public 方法上看到了显式路由。有没有办法确定哪个方法引发了不明确的路由错误?
当在控制器中声明方法 public 但没有 REST 属性时,可能会发生这种情况。将方法更改为受保护可能会解决问题。
我以前见过这种错误,通常错误消息指向罪魁祸首:
EBisAPI.Controllers._class.HandleError
我猜 HandleError
是你的基础 class 中的一个 public
方法,对吧?将其更改为 protected
并重试。
这当然只是一种可能的解决方案。如果错误消息中提到的方法是接口实现的一部分,则它不起作用,您需要查看其他解决方案之一。
Clean 方法可以使用数据注释 [NonAction]
而不是将您的方法设置为受保护。
类似于
[ApiExplorerSettings(IgnoreApi=true)]
(来自 https://github.com/domaindrivendev/Swashbuckle/issues/153 )
我通过用正确的 HTTP 属性修饰错误中提到的方法解决了这个错误。例如:[HttpGet]
此代码抛出错误:
public object Get()
{
MongoDbContext dbContext = new MongoDbContext();
return new {
API = true,
Database = dbContext.Status()
};
}
此代码有效:
[HttpGet]
public object Get()
{
MongoDbContext dbContext = new MongoDbContext();
return new {
API = true,
Database = dbContext.Status()
};
}
正如我在
- 用
[HttpGet("xxx")]
、[HttpPost("xxx")]
或...等显式 Http 方法装饰所有操作,而不是[Route("xxx")]
。 - 用
[NoAction]
属性修饰控制器中的 public 方法。
我遇到了同样的问题,是因为我重写了 Ok() 方法,返回了一个 OkObjectResult。解决方案是将其从 public 更改为 protected
我尝试了不同的解决方案,但对我有用的是如上所述在属性中添加路由。 I.E:[HttpPost("yourRoute/create")]
其中一个问题是我有 2 个 get 方法,所以我更改了它们的名称并按照我所说的将路由添加到属性。
我遇到了同样的问题。对我来说,错误是默认 WeatherForecastController
或任何其他 class 继承自 ControllerBase
class。但是当我创建一个新的时,它继承自 Controller
class.
所以 ControllerBase
和 Controller
class 之间的冲突导致了这个问题。
然后从 ControllerBase
继承新的控制器 class 为我解决了这个问题。
您不能同时继承 ControllerBase
和 Controller
。