Swashbuckle 未正确显示 API

Swashbuckle does not display the API correctly

我的项目中使用了Swashbuckle,按照文档进行操作,最后报错。

services.AddSwaggerGen(c =>
  {
    c.SwaggerDoc("v1", new OpenApiInfo
    {
        Version = "v1",
        Title = "ToDo API",
        Description = "A simple example ASP.NET Core Web API",
        TermsOfService = new Uri("https://example.com/terms"),
        Contact = new OpenApiContact
        {
            Name = "Shayne Boyer",
            Email = string.Empty,
            Url = new Uri("https://twitter.com/spboyer"),
        },
        License = new OpenApiLicense
        {
            Name = "Use under LICX",
            Url = new Uri("https://example.com/license"),
        }
    });
      });

    app.UseSwagger(c =>
    {
        c.SerializeAsV2 = true;
    });

app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
    });

我做错了什么?感谢您的帮助!

我以前遇到过这个问题,我可以重现你的问题,但我不确定这是不是你的问题。

所有配置遵循官方文档:

Get started with Swashbuckle and ASP.NET Core

那么这个错误的原因是必须指定请求方法,需要在方法中添加[HttpGet]、[HttpPost]等,通过Swagger可以查看API UI,像这样:

[ApiController]
public class HomeController : Controller
{
   
    [HttpGet]
    [Route("test")]
    public string Test()
    {
        return "Test";
    }
}

结果:

如果以上方法不能解决,能不能post你的具体错误信息?