不是的WebAPI即使有正确的返回XML接受头 [英] WebAPI not returning XML even with correct Accepts header

查看:125
本文介绍了不是的WebAPI即使有正确的返回XML接受头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.NET的WebAPI RC,和托管与没有什么花哨关于它的API控制器。一切工作正常使用JSON,但我测试使用接受请求头不同的格式,而这也正是我有麻烦了。

I'm using ASP.NET WebAPI RC, and hosting an API controller with nothing fancy about it. Everything works fine with JSON, but I'm testing requesting different formats using the Accepts header, and that's where I'm having trouble.

我使用jQuery发出一个AJAX请求,并设置要求的的dataType参数。这正确设置适当的Accept头你会看到下面。

I'm using jQuery to issue an AJAX request, and setting the 'dataType' parameter of the request. This correctly sets the appropriate Accept header as you will see below.

$.ajax({
    type: method,
    url: url,
    dataType: "xml",
    data: data || null,
    success: function (data) {
        // omitted
    }
});

下面是一个保存Fiddler请求/响应。正如你所看到的Accept头说,应用程序/ XML,但返回的WebAPI JSON。我自己也尝试手动设置Accept头只是应用程序/ XML(因此它不具有text / html的东西为好),但无济于事。

Here is a save of the fiddler request/response. As you can see the Accept header says application/xml, but WebAPI returned JSON. I have also tried manually setting the Accept header to just "application/xml" (so it doesn't have the text/html stuff as well), but to no avail.

什么鬼我缺少什么? (注:我的剪断的的一些数据信息保密,但没有以其他方式对其进行调整)

What the heck am I missing? (note: I've snipped some confidential info in the data but didn't tweak it otherwise)

GET http://localhost/insp**snip**6716 HTTP/1.1
Host: localhost
Connection: keep-alive
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.60 Safari/537.1
Accept: application/xml, text/xml, */*; q=0.01
Referer: http://localhost/inspector/api/test?
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: m=34e2:|2c69:t|47ba:t|4e99:t; .INSPECTOR3COOKIE=08BA683091E2A457B1832E9B*snip*F911D9ED97076


HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
Persistent-Auth: true
X-Powered-By: ASP.NET
Date: Fri, 03 Aug 2012 22:27:42 GMT
Content-Length: 1816

{"Id":2416716,"ProjectId":36,"Url":"http://ins *snip, but obviously this is JSON not XML *

我想指出,我不是在调整或AppStart的任何东西格式化,所以据我了解,在JSON和XML格式化应该是默认启用。

I'd like to point out I'm not tweaking any formatters in AppStart or anything, so as far as I understand, the JSON and XML formatters should be enabled by default.

更新:我想通了 - 检查以下

UPDATE: I figured it out -- check my own answer below

推荐答案

我想它了!

我在AppStart的有这个,因为我想的XML序列化不是DataContract序列化:

I had this in my AppStart, because I wanted the Xml serializer not the DataContract serializer:

GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;

但显然......有一些关于我的模型,使Xml序列认为它不能序列化。我猜测导致的WebAPI决定使用JSON格式来代替。

HOWEVER... apparently there is something about my model that makes the Xml Serializer think it can't serialize it. I am guessing that is causing WebAPI to decide to use the JSON formatter instead.

这是完全不直观,这貌似无害的设置可能会影响实际使用哪种格式。希望的WebAPI的人看到这一点:)

It's completely non-intuitive that this harmless looking setting could actually affect which formatter is used. Hope the WebAPI people see this :)

一些一种工具,让您了解内容协商过程的输入和输出,因此可以调试问题,这样就好了。

Some kind of tool that let you understand the inputs and outputs of content negotiation process so you can debug issues like this would be nice.

这篇关于不是的WebAPI即使有正确的返回XML接受头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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