Home > Net >  swagger not showing HttpGet query parameters
swagger not showing HttpGet query parameters

Time:04-21

I am working on asp.net core webapi(6.0) with swagger. When I run the application HttpGet method Parameters summary not visible in swagger. such as Id, StoreId, DateFrom, DateTo summary not display in swagger. Please advice

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

CodePudding user response:

Please modify your .csproj file like below.

<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>

Please comment builder.Services.AddSwaggerGen();, and =use below code.

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

It works for me, please check the result.

enter image description here

  • Related