如何仅使用swashbuckle在后摇请求的请求描述中隐藏属性?

问题描述 投票:0回答:1

我是ASP.NET Core的新手,这个问题看起来很简单,但是我无法在线找到合适的解决方案。这就是问题所在。这是我正在使用的类的结构。

public class Alert
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public string AlertId { get; set; }
    public string Type { get; set; }

}

这是对请求请求API的描述。

{
  "alertId": "string",
  "type": "string"
}

由于我在发布请求中使用[DatabaseGenerated(DatabaseGeneratedOption.Identity)]批注alertId是可选的。我的目的是仅在发布请求说明中隐藏alertId。我正在使用ASP.NET Core 3.1,EF Core(3.1.1)和Swashbuckle.AspDotNetCore(5.1.0)。请帮忙。谢谢。

c# asp.net-core swagger swashbuckle swashbuckle.aspnetcore
1个回答
0
投票

您可以将[JsonIgnore]属性添加到AlertId字段中,以确保发布请求不会获取AlertId的内容。

  public class Alert
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [JsonIgnore]
        public string AlertId { get; set; }
        public string Type { get; set; }

    }

这里是测试结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.