的ASP.NET Web API XmlFormatter不起作用,回落到JsonFormatter [英] ASP.NET Web API XmlFormatter does not work, falls back to JsonFormatter

查看:291
本文介绍了的ASP.NET Web API XmlFormatter不起作用,回落到JsonFormatter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在拉我的头发在过去两天试图弄清楚为什么XmlFormatter(通过DataContractSerializer的)不序列数据我在的Web API方法返回。的WebAPI决定无论如何使用JSON,但我需要的结果是XML(为每个应用程序将使用此API)。我安装我的浏览器发送接受:应用/ XML的解析器使用下,XMLFormatter(但结果始终是JSON)

I have been pulling my hair out for the past two days trying to figure out why XmlFormatter (via DataContractSerializer) does not serialize data I return in my Web API method. WebAPI decides to use JSON anyway but I need the result to be in XML (as per application that will use this API). I have setup my browser to send Accept: application/xml for the resolver to use the XmlFormatter (but the result is always json).

控制器:

public class MyController : ApiController
{
    public MyDataResultList GetData(string someArgument)
    {

        // magic here that gets the data
        MyDataResultList items = MyDataResultList.GetData(someArgument);

        return items;
    }
}

MyDataResultList被包含在一个dll并拥有本类似的布局:

MyDataResultList is contained in a dll and has this similar layout:

[DataContract]
[CollectionDataContract(Name = "MyDataList")]
[KnownType(typeof(List<MyDataItem>))]
public class MyDataResultList : List<MyDataItem>
{

    [DataMember]
    public string SomethingHere
    {
        get;
        set;
    }

    [DataMember]
    public TimeSpan StartTime
    {
        get;
        set;
    }

    [DataMember]
    public TimeSpan StopTime
    {
        get;
        set;
    }

}

我已经尝试设置的 UseXmlSerializer ,以真实的,但我需要使用DataContractSerializer的在客户端回到正确的反序列化的结果。

I have tried setting UseXmlSerializer to true, but I need to use the DataContractSerializer on the client end to de-serialize the results back correctly.

所以最终的问题是,它可以配置网络API抛出一个异常,如果它不能使用任何格式至上序列化?我认为(在我看来)这是非常误导,过于抽象有它只是悄悄的JSON没有给我任何线索,是什么原因造成的。

So the final question is, is it possible to configure web API to throw an exception if it is unable to serialize using whatever formatter comes first? I believe (in my opinion) it's very misleading and too abstractive to have it just silently fall back to JSON without giving me any clue as to what is causing that.

更新:使用DCS手动序列化MyDataResultList抛出InvalidDataContractEx​​ception:类型'MyDataResultList与CollectionDataContractAttribute属性是一个无效的集合类型,因为它具有DataContractAttribute属性。但潜在的问题仍然是:如何让网页API抛出这样对我,而不是默默回落至JSON? (与制作调试变得更加困难)

Update: manually serializing MyDataResultList using DCS throws InvalidDataContractException: Type 'MyDataResultList' with CollectionDataContractAttribute attribute is an invalid collection type since it has DataContractAttribute attribute. But the underlying question remains: how to get the Web API to throw this to me instead of silently falling back to JSON? (and making debugging more difficult)

UPDATE2: DataContract串行似乎完全即使他们有[数据成员跳到他们SomethingHere /开始时间/结束属性

Update2: DataContract serializer seems to skip SomethingHere/StartTime/EndTime properties entirely even though they have [DataMember] on them.

推荐答案

您的诊断方法是正确的,是的网页API内容的谈判进程将试图找到基于一堆逻辑的最佳格式(例如:如果$ P接受头$ psent,Content-Type头,如果接受,没有头present,让每个格式是否可以序列化一个类型等)。

Your way of diagnosis is correct and yeah Web API's content negotiation process will try to find the best formatter based on bunch of logic(ex: Accept header if present, Content-Type header if Accept-Header not present, asks each formatter if it can serialize a type etc.).

您可以禁用此默认行为(即找出其中可以写/序列化类型格式化程序列表中的第一格式)通过执行以下操作。这将导致一个 406无法接受响应正在生成:

You can disable this default behavior (i.e finding the first formatter in the list of formatters which can write/serialize a type) by doing the following. This will result in a 406 Not Acceptable response being generated:

DefaultContentNegotiator negotiator = new DefaultContentNegotiator(excludeMatchOnTypeOnly: true);
config.Services.Replace(typeof(IContentNegotiator), negotiator);

这篇关于的ASP.NET Web API XmlFormatter不起作用,回落到JsonFormatter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆