Swashbuckle Swagger生成实际的Guid [英] Swashbuckle Swagger generate an actual guid

查看:136
本文介绍了Swashbuckle Swagger生成实际的Guid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Swashbuckle在我的.Net Core WebAPI项目中生成Swagger.如下所示,它将生成字符串类型的GUID.我想生成一个随机Guid并将"string"替换为"或一个空的GUID"00000000-0000-0000-0000-000000000000".这将使我的示例在发布时可以真正发挥作用.

I am using Swashbuckle to generate Swagger in my .Net Core WebAPI project. As you can see below, it generates a GUID of type string. I would like to generate a random Guid and replace "string" with "" or an empty guid "00000000-0000-0000-0000-000000000000". This would allow my example to actually work when I post it.

{
  "payload": [
    {
      "GUID": "string",
      "status": "string"
    }
  ]
}

在我使用它的同时,是否可以对任何字符串使用相同的字符串,以使JSON每次都不同?

while I am at it, would it be possible to the same with any string so that JSON is different each time?

推荐答案

像这样装饰您的有效负载类中的GUID属性

Decorate your GUID property in your payload class like this

public class Payload
{
    /// <summary>
    /// The GUID
    /// </summary>
    /// <example>00000000-0000-0000-0000-000000000000</example>
    public string Guid { get; set; }
}

这会将示例从字符串"更改为"00000000-0000-0000-0000-0000-000000000000"

This should change the example from "string" to "00000000-0000-0000-0000-000000000000"

忘记添加.在您的startup.cs中,您可能需要添加以下代码

Forgot to add. In your startup.cs you might need to add the following code

        // Swagger
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new Info { Title = "TEST API", Version = "v1" });

            // Set the comments path for the Swagger JSON and UI.
            var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
            var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
            c.IncludeXmlComments(xmlPath);
        });

这篇关于Swashbuckle Swagger生成实际的Guid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆