我有一个使用 .NET MVC 4 在 C# 中实现的 Web 服务,但由于某种原因,尽管我发送了一个 Accept: application/json 标头,但它们总是以 XML 格式返回数据。有什么想法可能是错的吗?
提前致谢
更新:控制器代码如下所示
public HttpResponseMessage GetData()
{
...
DashboardInformation d = new DashboardInformation()
{
Name = "Test",
employees = employees,
shortcuts = shortcuts,
notes = dashboardNoteList,
timeEntries = dashboardTimeList,
timeTracker = timeTracker,
timeTrackerFromDate = ToUnixTimestamp(fromDate),
timeTrackerToDate = ToUnixTimestamp(toDate),
timeSummary = timeSummary,
userInfo = uInfo
};
return Request.CreateResponse(HttpStatusCode.OK, d);
}
更新 2:原始请求和响应
要求
GET /RestService/api/Dashboard/GetData HTTP/1.1
Host: xxx
Connection: keep-alive
Cache-Control: max-age=0
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4
Accept: application/json, text/javascript, */*; q=0.01
Referer: http://xxx/Firm4.0/dashboard.html
Accept-Encoding: gzip,deflate,sdch
Accept-Language: es-ES,es;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
回复
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/xml; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 06 Nov 2012 19:01:59 GMT
Content-Length: 3610
...