ABP.IO WEB應用程式框架 Swagger Default Value

  • 355
  • 0
  • ABP
  • 2023-06-12

筆記下 Swagger Input 提供 預設值 的方法

參照

Get started with Swashbuckle and ASP.NET Core | Microsoft Learn

產生 xml

YourProject.Application.Contracts.csproj

<PropertyGroup>
   <GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

設定 Swagger

YourProject.HttpApi.Host/YourProjectHttpApiHostModule.cs => ConfigureServices => ConfigureSwaggerServices

options =>
{
   // using System.Reflection;
   var xmlFilename = "YourProject.Application.Contracts.xml";
   options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
});

加上預設值

YourProject.Application.Contracts/YourDto.cs

[DefaultValue("HelloWorld")]
public string Message { get; set; }

[DefaultValue(new string[] { "a", "b" })] 
public List<string> Users { get; set; }
PS5