昂首阔步的回应说明未显示

时间:2020-05-28 10:41:43

标签: c# .net-core swagger swagger-ui

我目前正在尝试在Swagger UI中显示特定响应的描述,但似乎没有一个文档能够真正涵盖该问题的所有方面,以及我从{{3} }在.NET Core 3.1中无法使用...

        /// <summary>
        /// Test route...
        /// </summary>
        /// <returns></returns>
        /// <response code="200">This is a test</response>
        [HttpGet]
        [ProducesResponseType(typeof(string), StatusCodes.Status200OK)]
        public IActionResult Get()
        {
            return Ok("Hello World");
        }

我的.csproj也包含以下内容:

  <PropertyGroup>
    <GenerateDocumentationFile>true</GenerateDocumentationFile>
    <NoWarn>$(NoWarn);1591</NoWarn>
  </PropertyGroup>

Swagger UI最终看起来像这样(并且您可以看到,“ Descriptipn”列不应该包含“ This is a test”文本)。我想念什么吗?

Get started with Swashbuckle and ASP.NET Core

我还添加了[SwaggerResponse(StatusCodes.Status200OK, ...)],但没有任何变化。

2 个答案:

答案 0 :(得分:1)

在项目属性中,应检查在刀片sudo pm2 startup -u root 下发现的Output XML documentation file。然后在您的Build文件中:

startup

如果仍然不起作用,请检查xml文件是否出现在services.AddSwaggerGen(c => { //rest of your code //i'm using the default path generated, but you can adjust it as you want var XMLPath = AppDomain.CurrentDomain.BaseDirectory + nameof(MyNamespace) + ".xml"; if (File.Exists(XMLPath)) { c.IncludeXmlComments(XMLPath); } }); 文件夹中。如果没有,请检查Visual Studio中的属性,然后将bin调整为Copy to output directoryCopy

答案 1 :(得分:0)

事实证明,[SwaggerResponse]可以正常工作,但是在此之前,我需要在“启动”中“启用注释” ...

    services.AddSwaggerGen(config =>
    {
        config.SwaggerDoc("v1", new OpenApiInfo
        {
            Title = "Some API",
            Version = "v1"
        });

        config.EnableAnnotations();
    });