我在将我的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);
}
结果:
与此同时:
[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;
}
结果:
由于针对不同参数返回的数据结构不同,因此我无法返回已解析对象的列表。因此,在这一点上,我只接收原始的SOAP xml数据,并且必须对其进行解析/反序列化。但是要真正能够看到原始响应,我还需要将其显示为内容类型为application / xml的字符串中的内容。而且它给了我一个不错的输出(即使它仍然说只有application / json是可能的):
总结一下:
当将字符串解析为如下所示的实际格式时,我无法使用显示正确内容类型的响应内容类型(在“ Response Content Type” -filter中)。即使这会导致最重要的application / xml输出正确(请参见上面的屏幕截图);
[Produces("application/json", "application/xml")]
public ContentResult GetByAddress(string address)
{
return Content("<xml>...</xml>", "application/xml", Encoding.UTF8);
}
答案 0 :(得分:0)
这是因为返回类型会覆盖注释。要使其按需运行,您需要返回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);
}
答案 1 :(得分:0)
[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);
}
答案 2 :(得分:0)
我正在测试使用asp.net core2.2开发的Web api应用程序。
我安装了 Microsoft.AspNetCore.Mvc.Formatters.Xml nuget程序包。
Then I updated the **ConfigureServices** method within the Startup.cs by adding.
services.AddMvc().AddXmlSerializerFormatters();
并且我的Web api摇摇工具正在按我的需要工作,同时显示下拉列表(json和xml)以及预期的格式。 如果您需要更多信息,请访问:https://docs.microsoft.com/en-us/aspnet/core/web-api/advanced/formatting?view=aspnetcore-2.1