使用.NET Core 2 API实现Swashbuckle/Swagger时,访问swagger.json时出现500错误:
NotSupportedException: 操作存在模棱两可的HTTP方法 - EBisAPI.Controllers._class.HandleError(EBisAPI)。操作要求Swagger有明确的HttpMethod绑定
我已经查看了所有控制器并在每个控制器的公共方法上看到了明确的路由。有没有办法确定哪个方法会导致模糊路由错误?
使用.NET Core 2 API实现Swashbuckle/Swagger时,访问swagger.json时出现500错误:
NotSupportedException: 操作存在模棱两可的HTTP方法 - EBisAPI.Controllers._class.HandleError(EBisAPI)。操作要求Swagger有明确的HttpMethod绑定
我已经查看了所有控制器并在每个控制器的公共方法上看到了明确的路由。有没有办法确定哪个方法会导致模糊路由错误?
EBisAPI.Controllers._class.HandleError
。我猜HandleError
是您基类中的一个公共方法,对吧?将其更改为protected
,然后再试一次。当然,这只是一种可能的解决方案。如果在错误消息中提到的方法是接口实现的一部分,则它无效,您需要查看其他解决方案之一。与totonho的答案类似,你可以使用
[ApiExplorerSettings(IgnoreApi=true)]
(来自https://github.com/domaindrivendev/Swashbuckle/issues/153)
清晰的方法是使用数据注释[NonAction]
来将您的方法设置为受保护状态。
IActionFilter
方法更改为 protected 导致以下错误:Severity Code Description Project File Line Suppression State Error CS0737 'OrderController' does not implement interface member 'IActionFilter.OnActionExecuting(ActionExecutingContext)'. 'OrderController.OnActionExecuting(ActionExecutingContext)' cannot implement an interface member because it is not public.
然而,**[NonAction]
** 解决了问题。 - Sujoy[Route("error")] [NonAction] [AllowAnonymous] public IActionResult Error() { ... }
中添加 [NonAction]
后,我收到了 404 错误。 ASP.NET Core 找不到我的 /error
端点。我投反对票。 - Exegesis我通过为错误中提到的方法使用正确的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()
};
}
正如我在"Swashbuckle not creating swagger.json file"中提到的:
[HttpGet("xxx")]
、[HttpPost("xxx")]
等,而不是使用[Route("xxx")]
。[NonAction]
属性。[NoAction]
进行装饰。这种情况很少见,但在我尚未遇到的特殊情况下可能需要它(例如,使用反射反射公共方法或...)。@Jorn.Beyers - Mohammad Barbast我尝试了不同的解决方案,但对我有效的是按照上述方式在属性中添加路由。例如:[HttpPost("yourRoute/create")]
其中一个问题是我有2个GET方法,所以我改变了它们的名称,并像我说的那样在属性中添加了路由。
我曾经遇到过同样的问题。对我来说,错误是默认情况下WeatherForecastController
或任何其他类都继承自ControllerBase
类。但是当我创建一个新的控制器时,它继承自Controller
类。
因此,ControllerBase
和Controller
类之间的冲突导致了这个问题。
然后,将新控制器从ControllerBase
类继承解决了我的问题。
您不能同时从ControllerBase
和Controller
继承。
[HttpGet]
属性。哎呀。 - mwilson严重程度 代码 描述 项目 文件 行 抑制状态 错误 CS0737 “OrderController”未实现接口成员“IActionFilter.OnActionExecuting(ActionExecutingContext)”。“OrderController.OnActionExecuting(ActionExecutingContext)”无法实现接口成员,因为它不是公共的。
- Sujoy