Swashbuckle/Swagger + ASP.Net Core:“无法加载 API 定义"; [英] Swashbuckle/Swagger + ASP.Net Core: "Failed to load API definition"

查看:56
本文介绍了Swashbuckle/Swagger + ASP.Net Core:“无法加载 API 定义";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个 ASP.NET Core 2 应用程序并包含了 Swagger.一切正常,直到我引入了一个没有明确定义 HTTP 操作的方法:

I develop an ASP.NET Core 2 application and included Swagger. Everything worked fine until I introduced a method without explicitly defining the HTTP action:

public class ErrorController : Controller
{
    [Route("/error")]
    public IActionResult Index()
    {
        return StatusCode(500, new Error("Internal error."));
    }
}

当我使用这种方法启动应用程序时,出现以下消息:

When I started the app with this method, the following message showed up:

未能加载 API 定义.

Failed to load API definition.

错误
获取错误 内部服务器错误/swagger/v1/swagger.json

Errors
Fetch error Internal Server Error /swagger/v1/swagger.json

只要我明确设置例如[HttpGet] 错误消失.问题是,我需要这个方法来触发所有可能的 HTTP 操作.当然,我可以明确指定所有操作,但我感觉 Swagger 应该能够正确处理这个问题.

As soon as I explicitly set e.g. [HttpGet] the error disappears. The problem with this is, I need this method to fire for all possible HTTP operations. Of course, I could specify all operations explicitly, but I have the feeling Swagger should be able to handle this correctly.

为什么 Swagger 会这样?

Why does Swagger behave this way?

有我可以使用的配置吗?

Is there any configuration I can use?

推荐答案

选项 ResolveConflictingActions 应该正在处理这种情况...

The option ResolveConflictingActions should be working on this case...

这里是实际的错误:

System.NotSupportedException: Ambiguous HTTP method for action

来自:githubrel="://github..com/domaindrivendev/Swashbuckle.AspNetCore/blob/86cc761bc4f5dda796f80ad8dfd8bc205542a6f6/src/Swashbuckle.AspNetCore.SwaggerGen/Generator/SwaggerGenerator.cs#L90

我认为这是一个错误,如果你真的有兴趣,你应该向项目报告

I think this is a bug, if you are truly interested you should report it to the project

这篇关于Swashbuckle/Swagger + ASP.Net Core:“无法加载 API 定义";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆