gpt4 book ai didi

c# - Swagger 响应描述未显示

转载 作者:行者123 更新时间:2023-12-04 09:47:54 25 4
gpt4 key购买 nike

我目前正在尝试在 Swagger UI 中显示特定响应的描述,但似乎没有真正涵盖所有方面的文档,以及我从 Get started with Swashbuckle and ASP.NET Core 尝试的所有示例在 .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”文本)。我错过了什么吗?

Image preview

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

最佳答案

在您的项目属性中,您应该检查 Output XML documentation file在 Blade 下发现 Build .然后在您的 startup文件:

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);
}
});

如果仍然不起作用,请检查 xml 文件是否出现在 bin 中。文件夹。如果没有检查 Visual Studio 中的属性并调整 Copy to output directoryCopyCopy if newer .

关于c# - Swagger 响应描述未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62062751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com