【问题标题】:Status code 406 using Format response data in ASP.NET Core Web API使用 ASP.NET Core Web API 中的格式响应数据的状态码 406
【发布时间】:2020-10-09 12:27:55
【问题描述】:

我正在尝试格式化来自我遵循 Microsoft 教程的 GET 方法的响应。我可以获取 json 响应,但不能获取 xml,它返回状态代码 406。

这是我的 Startup 课程:

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>();
        });

控制器:

[Route("api/[controller]")]
[ApiController]
[FormatFilter]
public class TableController : ControllerBase
{
[HttpGet("values.{format}")]
    public ActionResult<List<Table>> Get()
    {
        return _context.Table.ToList();
    }
}

【问题讨论】:

    标签: c# api asp.net-core asp.net-web-api


    【解决方案1】:

    我正在回答我自己的问题,因为我找到了我只需要替换的答案

    AddXmlSerializerFormatters()

    AddXmlDataContractSerializerFormatters()

    【讨论】:

      猜你喜欢
      • 2018-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-29
      • 2018-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多