Swagger - 如何在 C# 中为子对象添加定义
Swagger - How to add definitions on child objects in c#
在 C#(Asp.Net 核心)中,我有以下关于 NameValue.
类型的子对象的文档
我希望在 FromWarehouse 上添加我在此处添加的文档,但是当 Swagger 呈现时,它使用 NameValue 的定义,而不是来自仓库.
/// <summary>
/// The warehouse this transfer is coming from
/// </summary>
/// <remarks>GLW, WDI, PSO, SMA, SHW</remarks>
public NameValue FromWarehouse { get; set; }
你需要做三件事:
- 摘要应从 class 属性 移至 class。
这里没有
public NameValue FromWarehouse { get; set; }
但是为了
/// <summary>
/// The warehouse this transfer is coming from
/// </summary>
/// <remarks>GLW, WDI, PSO, SMA, SHW</remarks>
public class NameValue{
....
- 您需要添加文档生成,包含Xml评论
services.AddSwaggerGen(c =>
{
var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
});
- 将此添加到您的 .csproj 文件
<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
这是我用的例子
/// <summary>
/// The warehouse this transfer is coming from
/// </summary>
/// <remarks>GLW, WDI, PSO, SMA, SHW</remarks>
public class NameValue
{
/// <summary>
/// The Name
/// </summary>
public string Name { get; set; }
/// <summary>
/// The Value
/// </summary>
public string Value { get; set; }
}
结果如下:
备注,我无法包含,但据我了解,备注用于提供有效负载示例,并且在资源方法之上使用。
我在 dotnet 6 和 Swashbuckle.AspNetCore 版本 6.2.3 中使用了所有这些。
希望对您有所帮助。
Reference 至 Microsoft 文档。
在 C#(Asp.Net 核心)中,我有以下关于 NameValue.
类型的子对象的文档我希望在 FromWarehouse 上添加我在此处添加的文档,但是当 Swagger 呈现时,它使用 NameValue 的定义,而不是来自仓库.
/// <summary>
/// The warehouse this transfer is coming from
/// </summary>
/// <remarks>GLW, WDI, PSO, SMA, SHW</remarks>
public NameValue FromWarehouse { get; set; }
你需要做三件事:
- 摘要应从 class 属性 移至 class。
这里没有
public NameValue FromWarehouse { get; set; }
但是为了
/// <summary>
/// The warehouse this transfer is coming from
/// </summary>
/// <remarks>GLW, WDI, PSO, SMA, SHW</remarks>
public class NameValue{
....
- 您需要添加文档生成,包含Xml评论
services.AddSwaggerGen(c =>
{
var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
});
- 将此添加到您的 .csproj 文件
<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
这是我用的例子
/// <summary>
/// The warehouse this transfer is coming from
/// </summary>
/// <remarks>GLW, WDI, PSO, SMA, SHW</remarks>
public class NameValue
{
/// <summary>
/// The Name
/// </summary>
public string Name { get; set; }
/// <summary>
/// The Value
/// </summary>
public string Value { get; set; }
}
结果如下:
备注,我无法包含,但据我了解,备注用于提供有效负载示例,并且在资源方法之上使用。
我在 dotnet 6 和 Swashbuckle.AspNetCore 版本 6.2.3 中使用了所有这些。
希望对您有所帮助。
Reference 至 Microsoft 文档。