C# 根据模型使用Swagger生成示例请求

C# 根据模型使用Swagger生成示例请求,c#,.net,.net-core,swagger,swashbuckle,C#,.net,.net Core,Swagger,Swashbuckle,大家好。请告诉我。我是否可以根据模型在Swagger中生成示例请求,以避免手动编写。查看模型具有哪些字段。 甚至可能吗? 因为现在我必须手动为每个api编写请求的描述。 我想自动化这个过程。 我使用的是Swashback和ASP.NET Core这里有代码示例,您应该在模型上添加///123,而不是注释 这是否接近您需要的: 代码如下: 您还可以使用IDocumentFilter进行动态更改,如下所示: private class AddExampleDocumentFilter

大家好。请告诉我。我是否可以根据模型在Swagger中生成示例请求,以避免手动编写。查看模型具有哪些字段。
甚至可能吗?
因为现在我必须手动为每个api编写请求的描述。
我想自动化这个过程。
我使用的是Swashback和ASP.NET Core

这里有代码示例,您应该在模型上添加
///123
,而不是注释

这是否接近您需要的:

代码如下:


您还可以使用
IDocumentFilter
进行动态更改,如下所示:

    private class AddExampleDocumentFilter : IDocumentFilter
    {
        private List<Guid> Guids
        {
            get
            {
                return new List<Guid>
                {
                    Guid.Empty, Guid.Empty
                };
            }
        }

        public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry s, IApiExplorer a)
        {
            if (swaggerDoc.paths.ContainsKey("/api/Dictionary"))
            {
                var del = swaggerDoc.paths["/api/Dictionary"].delete;
                if (del != null)
                {
                    del.parameters[0].schema.example = Guids;
                }
            }
        }
    }
私有类AddExampleDocumentFilter:IDocumentFilter
{
私有列表GUID
{
得到
{
返回新列表
{
Guid.Empty,Guid.Empty
};
}
}
公共无效应用(Swagger文档Swagger文档、SchemaRegistry s、IApiExplorer a)
{
if(swaggerDoc.path.ContainsKey(“/api/Dictionary”))
{
var del=swaggerDoc.path[“/api/Dictionary”]。删除;
如果(del!=null)
{
del.parameters[0].schema.example=Guids;
}
}
}
}
结果如下:

一旦用户使用示例数据执行它,UI就会显示curl请求:

有一个放置代码示例的地方,您应该在模型上添加
///123
,而不是添加注释

这是否接近您需要的:

代码如下:


您还可以使用
IDocumentFilter
进行动态更改,如下所示:

    private class AddExampleDocumentFilter : IDocumentFilter
    {
        private List<Guid> Guids
        {
            get
            {
                return new List<Guid>
                {
                    Guid.Empty, Guid.Empty
                };
            }
        }

        public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry s, IApiExplorer a)
        {
            if (swaggerDoc.paths.ContainsKey("/api/Dictionary"))
            {
                var del = swaggerDoc.paths["/api/Dictionary"].delete;
                if (del != null)
                {
                    del.parameters[0].schema.example = Guids;
                }
            }
        }
    }
私有类AddExampleDocumentFilter:IDocumentFilter
{
私有列表GUID
{
得到
{
返回新列表
{
Guid.Empty,Guid.Empty
};
}
}
公共无效应用(Swagger文档Swagger文档、SchemaRegistry s、IApiExplorer a)
{
if(swaggerDoc.path.ContainsKey(“/api/Dictionary”))
{
var del=swaggerDoc.path[“/api/Dictionary”]。删除;
如果(del!=null)
{
del.parameters[0].schema.example=Guids;
}
}
}
}
结果如下:

一旦用户使用示例数据执行它,UI就会显示curl请求:

就我个人而言,我认为大摇大摆就足够了。斯威格还增加了一些模型,帮助读者。读者还可以直接在swagger上执行请求(然后查看实际请求)。因此,我认为没有必要手动描述每个调用。除此之外,您还可以使用生成客户感谢您的回答,但在我的情况下,这仍然是必要的。因此,我决定看看是否有可能简化这一过程)我个人认为大摇大摆就足够了。斯威格还增加了一些模型,帮助读者。读者还可以直接在swagger上执行请求(然后查看实际请求)。因此,我认为没有必要手动描述每个调用。除此之外,您还可以使用生成客户感谢您的回答,但在我的情况下,这仍然是必要的。因此,我决定看看是否有可能简化这个过程)谢谢你的回答,我将尝试申请。谢谢你的回答,我将尝试申请。