gpt4 book ai didi

asp.net-core - 大摇大摆不显示 HttpGet 查询参数

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

我正在使用 swagger 开发 asp.net core webapi(6.0)。当我运行应用程序 HttpGet 方法时,Parameters 摘要在 swagger 中不可见。诸如 Id、StoreId、DateFrom、DateTo 摘要不会在 swagger 中显示。请指教

 /// <summary>
/// Get product details.
/// </summary>
/// <remarks>Fetches product.</remarks>
/// <param name="parameters"></param>
/// <returns></returns>
/// <response code="200">Successful Response</response>
/// <response code="400">Bad Request</response>
/// <response code="401">Unauthorized</response>
/// <response code="424">Failed Dependency</response>
/// <response code="500">Internal Server Error</response>
public async Task<IActionResult> GetProduct([FromQuery] Parameter parameters)
{
var repo = await Repository.GetAllAsync(parameters);
return Ok()
}



public class Parameter
{
/// <summary>
/// Product Id
/// </summary>
public string? Id { get; set; }
/// <summary>
/// Optional Parameter. If supplied, result set will be restricted to the products from the supplied store alone
/// </summary>
public int? StoreId { get; set; }
/// <summary>
/// Optional Parameter.
/// </summary>
public DateTime? DateFrom { get; set; }
/// <summary>
/// Optional Parameter.
/// </summary>
public DateTime? DateTo { get; set; }
}

最佳答案

XML comments可用于向 swagger UI 添加参数。

首先在你的 .csproj 文件中启用 GenerateDocumentationFile 以生成 xml 文档文件,该文件将在 swagger json 生成中使用。

<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>

</Project>

然后从生成的 xml 文档文件中将 swagger 服务配置为 IncludeXmlComments:

//builder.Services.AddSwaggerGen();
builder.Services.AddSwaggerGen(options =>
{
// using System.Reflection;
var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
});

结果: enter image description here

关于asp.net-core - 大摇大摆不显示 HttpGet 查询参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71911107/

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