将模型信息添加到swagger输出

时间:2016-05-12 19:00:26

标签: c# swagger swashbuckle

有没有办法在swagger输出中添加模型信息,如有效值,默认值,摘要和其他注释?

例如在c#中,我如何将以下注释和属性添加到swagger中?

/// <summary>
/// A clear summary
/// </summary>
/// <remarks>
/// Some remarks
/// </remarks>
public class A
{
    public A()
    {
        _Field_A = 0;
        _Field_B = string.Empty;
    }

    private int _Field_A { get; set; }

    [Range(0, 150)]
    public int Field_A
    {
        get
        {
            return _Field_A;
        }

        set
        {
            if (value != null) { _Field_A = value; }
        }
    }

    private string _Field_B { get; set; }

    /// <summary>
    /// Field_B summary
    /// </summary>    
    public string Field_B
    {
        get
        {
            return _Field_B;
        }

        set
        {
            if (value != null) { _Field_B = value; }
        }
    }
}

2 个答案:

答案 0 :(得分:4)

您需要在项目属性中启用XML文档文件创建: 项目属性&gt;构建&gt;检查XML文档文件框

然后,您可以取消注释或将以下行添加到SwaggerConfig.cs文件中: c.IncludeXmlComments(GetXmlCommentsPath());

答案 1 :(得分:2)

根据Swashbuckle github,您可以启用XML注释,以便相应地添加元数据。

httpConfiguration
    .EnableSwagger(c =>
        {
            c.SingleApiVersion("v1", "A title for your API");
            c.IncludeXmlComments(GetXmlCommentsPathForControllers());
            c.IncludeXmlComments(GetXmlCommentsPathForModels());
        });