【问题标题】:Swashbuckle.AspNetCore.Filters doesn't recognize my JsonPropertyNamesSwashbuckle.AspNetCore.Filters 无法识别我的 JsonPropertyNames
【发布时间】:2022-01-23 03:22:58
【问题描述】:

我正在 .net-core 3.1 中开发 Web API

我目前正在使用 Swashbuckle.AspNetCore.Filters 来展示 JSON 日期格式的特定示例。但是,当我使用这个库时,它并没有取[JsonPropertyName]的值。 我的 JSON 示例当前如下所示:

{
  "contractCode": 0,
  "registers": 0,
  "totalDue1": 0,
  "totalDue2": 0,
  "buildingExpenses": [
    {
      "ownersAssociationCode": 0,
      "functionalUnitCode": 0,
      "period": "string",
      "ownerName": "string",
      "location": "string",
      "ownerEmail": "string",
      "dueDate1": "21/12/2021",
      "amount1": 0,
      "dueDate2": "21/12/2021",
      "amount2": 0,
      "electronicPaymentCode": "string",
      "barcode": "string"
    }
  ]
}

在应用 Swashbuckle 之前,它看起来像这样:

{
  "Cliente": 0,
  "CantidadRegistros": 0,
  "TotalesPrimerVencimiento": 0,
  "TotalesSegundoVencimiento": 0,
  "Detalle": [
    {
      "Consorcio": 0,
      "UnidadFuncional": 0,
      "Periodo": "string",
      "Propietario": "string",
      "Ubicacion": "string",
      "Email": "string",
      "FechaPrimerVencimiento": "2022-12-10",
      "ImportePrimerVencimiento": 0,
      "FechaSegundoVencimiento": "2021-12-20",
      "ImporteSegundoVencimiento": 0,
      "CodigoDePagoElectronico": "string",
      "CodigoDeBarras": "string"
    }
  ]
}

我希望它现在也像这样,保留当前应用的示例日期格式 (dd/MM/yyyy)。

根据我阅读的内容,我必须安装 Swashbuckle.AspNetcore.Newtonsoft 包并将其添加到我的状态中,但这对我不起作用。

services.AddSwaggerGenNewtonsoftSupport();

这是我使用的 Dto,我在其中应用 JsonPropertyName:

public class BuildingExpenseResumeInputDto
    {
        [JsonPropertyName("Cliente")]
        public int ContractCode { get; set; }

        [JsonPropertyName("CantidadRegistros")]
        public int Registers { get; set; }

        [JsonPropertyName("TotalesPrimerVencimiento")]
        public decimal TotalDue1 { get; set; }

        [JsonPropertyName("TotalesSegundoVencimiento")]
        public decimal TotalDue2 { get; set; }

        [JsonPropertyName("Detalle")]
        public List<BuildingExpenseDetailInputDto> BuildingExpenses { get; set; }

        public BuildingExpenseResumeInputDto()
        {
            BuildingExpenses = new List<BuildingExpenseDetailInputDto>();
        }
    }

希望你能帮我解决!

【问题讨论】:

    标签: c# asp.net-core swashbuckle


    【解决方案1】:

    我终于通过将我的 Swashbuckle 版本从 5.0 更新到 6.2.3 并将这个属性应用到我的 Dto 而无需使用 Swashbuckle.Filters 来解决它:

    [DefaultValue("31/05/2022")]
    

    【讨论】:

      猜你喜欢
      • 2012-12-26
      • 2011-08-22
      • 2010-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-07
      • 2017-05-08
      • 2017-01-19
      相关资源
      最近更新 更多