swagger、.net core api 中的响应内容类型

Sge*_*dda 7 c# swagger asp.net-core

我在 swagger 中将 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>() },
        });
    });
}
Run Code Online (Sandbox Code Playgroud)

和行动:

[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);
}
Run Code Online (Sandbox Code Playgroud)

与以下结果相同:

[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);
}
Run Code Online (Sandbox Code Playgroud)

结果是:

结果在这 当单击“执行”时,我得到了正确的 XML 格式的结果。

虽然这样:

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

或这个:

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

结果是:

在此输入图像描述

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

在此输入图像描述

总结一下:

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

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

Miq*_*Miq 5

这是因为返回类型覆盖了注释。为了让它按您希望的方式工作,您需要返回 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);
}
Run Code Online (Sandbox Code Playgroud)

招摇输出