【问题标题】:Response content types in swagger, .net core apiswagger,.net core api中的响应内容类型
【发布时间】:2019-05-25 11:22:01
【问题描述】:

我在大摇大摆地将我的 api 响应呈现为 xml 时遇到了一些问题,最后我用下面的第一个操作将它呈现为正确的格式 application/xml,但它仍然说我只能将它呈现为 application/json .

我试图从 Produces 属性中删除 application/json,但仍然只显示 application/json。

任何想法为什么它会以这种方式表现?

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc().AddMvcOptions(o => o.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter()));
    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
        c.RoutePrefix = "";
    });
    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
        c.AddSecurityDefinition("Bearer", new ApiKeyScheme { In = "header", Description = "Please enter JWT with Bearer into field", Name = "Authorization", Type = "apiKey" });
        c.AddSecurityRequirement(new Dictionary<string, IEnumerable<string>> {
            { "Bearer", Enumerable.Empty<string>() },
        });
    });
}

和行动:

[Produces("application/json", "application/xml")]
public ContentResult GetByAddress(string address)
{
    var addressId = GetAddressIdByAddress(address);
    string result = _soapApi.GetResultById(addressId);
    return Content(result, "application/xml", Encoding.UTF8);
}

结果相同:

[Produces("application/json", "application/xml")]
public IActionResult GetByAddress(string address)
{
    var addressId = GetAddressIdByAddress(address);
    string result = _soapApi.GetResultById(addressId);
    return Content(result, "application/xml", Encoding.UTF8);
}

结果:

当点击执行时,我得到了正确的 XML 格式的结果。

此时:

[Produces("application/json", "application/xml")]
public string GetByAddress(string address)
{
    var addressId = GetAddressIdByAddress(address);
    string result = _soapApi.GetResultById(addressId);
    return result;
}

或者这个:

[Produces("application/json", "application/xml")]
public List<Address> GetByAddreses()
{
    string result = _soapApi.GetResults();
    return result;
}

结果:

由于为不同的参数返回不同的数据结构,我无法返回已解析对象的列表。所以此时我只收到原始肥皂 xml-data 并且必须对其进行解析/反序列化。但为了真正能够看到原始响应,我还需要将其显示为内容类型为 application/xml 的字符串中的内容。它给了我一个很好的输出,比如(尽管它仍然说只有 application/json 是可能的):

总结一下:

当字符串被解析为如下所示的实际格式时,我没有让它与显示正确内容类型的响应内容类型一起工作(在“响应内容类型”-过滤器中)。即使这会导致最重要的应用程序/xml 的正确输出(参见上面的屏幕截图);

[Produces("application/json", "application/xml")]
public ContentResult GetByAddress(string address)
{
    return Content("<xml>...</xml>", "application/xml", Encoding.UTF8);
} 

【问题讨论】:

标签: c# asp.net-core swagger


【解决方案1】:

这是因为返回类型覆盖了注解。要让它按您的意愿工作,您需要返回 IActionResult 并使用注释来提供特定代码的响应类型模型。
所以改为返回result,返回Ok(result)

我创建了示例代码,它使用 swagger 4.0.1 对我有用

[HttpGet("Get2")]
[Produces("application/json", "application/xml", Type = typeof(List<string>))]
public IActionResult Get2()
{
   var list = new List<string>() { "value1", "value2" };
   return Ok(list);
}

【讨论】:

  • 很遗憾没有帮助
  • @Sgedda - 我更新了我的答案,请检查这是不是你想要的。
  • 我更新了我的问题,提供了一些关于我的案例的更多信息。这不是一个大问题,我设法解决的重要问题是让它正确格式化以作为响应。即使它仍然在响应类型中显示“application/json”。
【解决方案2】:
[Produces("application/json", "application/xml")]
public Task<IActionResult> GetByAddreses()
{
    var result = _soapApi.GetResults();
var response = ResponceMethod<List<Address>>.SuccessResponse(StatusCodes.Status200OK, "Success", result);
    return Ok(response);
}

【讨论】:

  • 试试这个方法
  • 这无济于事,在我的情况下,我没有像 Address 这样的映射对象,因为 api 返回大量数据并需要原始输出。这部分似乎错误,无法编译:ResponceMethod>.SuccessResponse
  • 你可以使用 simplay var result = _soapApi.GetResults();返回确定(结果);
  • 我更新了问题,问题仍然存在,但我至少可以得到正确的输出。
【解决方案3】:

我正在测试一个使用 asp.net core2.2 开发的 web api 应用程序。

我安装了 Microsoft.AspNetCore.Mvc.Formatters.Xml nuget 包。

然后我通过添加更新了 Startup.cs 中的 ConfigureServices 方法。

services.AddMvc().AddXmlSerializerFormatters();

我的 web api swagger 正在按我的需要工作,下拉菜单同时显示(json 和 xml)并产生预期的格式。 如果您需要更多信息,请访问:https://docs.microsoft.com/en-us/aspnet/core/web-api/advanced/formatting?view=aspnetcore-2.1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-10
    • 2019-10-09
    • 1970-01-01
    • 1970-01-01
    • 2017-11-10
    • 2015-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多