【问题标题】:Swashbuckle Swagger C# WebApi Core 2.0 - "Produces" is emptySwashbuckle Swagger C# WebApi Core 2.0 - “Produces”为空
【发布时间】:2018-02-10 10:35:41
【问题描述】:

我很难找到错误。 我用 .net Core 2 和 Swagger 设置了一个小的 Web API。 添加 XML 支持 (.AddXmlDataContractSerializerFormatters();) 后,swagger 不会在 UI 中显示“响应内容类型”选项。

我还将 "[Produces("application/json", "application/xml")]" 设置为装饰器。 然而生成的 json 总是打印出来:

"consumes":[  

],
"produces":[  

],

所以我不确定我在这里做错了什么。

    [HttpGet("Strom/{plz}")]
    [Produces("application/json", "application/xml")]
    public IActionResult GetStrom(string plz)
    {
        int iplz = 0;

        if (plz.Length != 5 || !int.TryParse(plz, out iplz))
        {
            return BadRequest("Die Postleitzahl ist ungültig.");
        }
        return Ok(GetOrte(plz, 1));
    }

【问题讨论】:

    标签: c# swagger asp.net-core-2.0 swashbuckle swagger-codegen


    【解决方案1】:

    由于我使用的是 IActionResult,我需要确保说明它获取的是什么类型的内容...

    [ProducesResponseType(typeof(IEnumerable<Model.Ort.Ort>),200)]
    

    确实做到了。所以一定要正确地装饰你的 api...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-29
      • 1970-01-01
      • 1970-01-01
      • 2018-02-01
      • 2019-09-20
      • 1970-01-01
      相关资源
      最近更新 更多