[英]Status code 406 using Format response data in ASP.NET Core Web API
I'm trying to format the response from a GET method i followed the Microsoft tutorial.I can get json response but not xml it returns a status code 406.我正在尝试格式化来自我遵循Microsoft教程的 GET 方法的响应。我可以获得 json 响应但不是 xml 它返回状态代码 406。
This is my Startup class:这是我的启动 class:
services.AddControllers()
.AddXmlSerializerFormatters();
services.AddControllers()
.AddNewtonsoftJson();
services.AddControllers().AddNewtonsoftJson(options =>
{
// Use the default property (Pascal) casing
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
});
services.AddControllers(options =>
{
// requires using Microsoft.AspNetCore.Mvc.Formatters;
options.OutputFormatters.RemoveType<StringOutputFormatter>();
options.OutputFormatters.RemoveType<HttpNoContentOutputFormatter>();
});
Controller: Controller:
[Route("api/[controller]")]
[ApiController]
[FormatFilter]
public class TableController : ControllerBase
{
[HttpGet("values.{format}")]
public ActionResult<List<Table>> Get()
{
return _context.Table.ToList();
}
}
I'm answering my own question because i found the answer i just needed to replace the我正在回答我自己的问题,因为我找到了我只需要替换的答案
AddXmlSerializerFormatters()
AddXmlSerializerFormatters()
with和
AddXmlDataContractSerializerFormatters()
AddXmlDataContractSerializerFormatters()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.