【问题标题】:.Net Core 2 Accept Header of XML returning 406.Net Core 2 Accept Header 的 XML 返回 406
【发布时间】:2018-05-11 02:23:12
【问题描述】:

我为我的 API 解决方案添加了格式化输出和 xml 格式输入

//add formatter to support XML media type results(application/xml)
setupAction.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());
//add formatter to support XML media type request(application/xml)
setupAction.InputFormatters.Add(new XmlDataContractSerializerInputFormatter());

但是当我使用 application/xml 的接受标头发出请求时,我得到一个 406 有其他人遇到过这个吗?

内容类型为application/json

---- 已修复 ----

如果控制器操作返回的对象有一个构造函数,并且接受头是 application/xml,那么响应将是 406。只需删除构造函数,然后我就可以返回 XML。

【问题讨论】:

  • 有些情况是控制器有 [Produces("application/json")]
  • 在我的例子中,XML 序列化不能序列化 ICollection。必须定义为 List。弱酱。

标签: xml rest api asp.net-core-2.0 request-headers


【解决方案1】:
Actually, I had the same problem, In my case I have a relationship in my tables, so, Entity Framework create a IEnumerable<Class> when you have HasMany, so what I did was just change my code 
from:
public ICollection<Credential> Credential { get; set; }

to:
public List<Credential> Credential { get; set; }

and Constructor from:
        public Personal()
        {
            Credential = new HashSet<Credential>();
        }
to:
        public Personal()
        {
            Credential = new List<Credential>();
        }

【讨论】:

    【解决方案2】:

    我遇到了同样的问题(.Net Core 2.2)。

    由于此页面上的注释:https://docs.microsoft.com/en-us/aspnet/core/web-api/advanced/formatting?view=aspnetcore-2.2

    我检查了我的控制器继承自 Controller 并且我的方法正在返回 IActionResult。就是这样。

    起初我添加了这个输出格式化程序:

      setupAction.OutputFormatters.Add(new XmlSerializerOutputFormatter());
    

    尽管格式化程序在 outputformatters 列表中并且具有正确的 MediaType,但这不起作用。

    然后我改为使用首选的 .Net 2.2 方式:

            services.AddMvc(setupAction => 
            { 
                   ...
            })
            .AddXmlSerializerFormatters();
    

    还是没有成功。

    我回到“旧”方式并删除了 AddXmlSerializerFormatters() 并添加了

     setupAction.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());
    

    即使用 XmlDataContractSerializerOutputFormatter 而不是 XmlSerializerOutputFormatter。

    然后它起作用了。

    我花了一些时间来找出不同之处,我的猜测是 XmlSerializerOutputFormatter 可以编写 IEnumerable 的对象类型,而 Customer 是没有构造函数的 POCO。

    这是使用 XmlSerializerOutputFormatter 在日志中的内容 Microsoft.AspNetCore.Mvc.Infrastructure.DefaultOutputFormatterSelector:警告:未找到内容类型“应用程序/xml”的输出格式化程序以写入响应。 Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor:警告:找不到内容类型“application/xml”的输出格式化程序来写入响应。

    【讨论】:

    • 我的班级有一个没有参数的构造函数,但除了这个setupAction.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());之外什么都没有,所以谢谢!
    【解决方案3】:

    重新。 “如果控制器操作返回的对象具有构造函数并且接受标头是 application/xml,则响应将为 406。”实际上,这是不正确的。更正:“如果控制器操作返回的对象有一个构造函数接受参数,并且该对象也没有 0 参数构造函数,并且接受头是 application/xml,那么响应将是406。”

    【讨论】:

    • 开什么玩笑0.o
    猜你喜欢
    • 1970-01-01
    • 2017-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    相关资源
    最近更新 更多