如何设置的ASP.NET Web API默认的序列化? [英] How to set the default serializer in ASP.NET Web API?

查看:128
本文介绍了如何设置的ASP.NET Web API默认的序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在看有关的ASP.NET Web API的视频课程。当控制器被调用,数据获取JSON通过默认返回。我只是想知道,因为当我从视频复制此示例项目,我得到XML。

I'm currently watching a video course about ASP.NET Web API. When a controller gets called, the data gets returned in JSON by default. I was just wondering, because when I copy this sample project from the video, I get XML.

挫折是很大的,请帮我解决这个问题。

The frustration is big, please help me to solve this.

我是pretty新的ASP.NET Web API,所以请多多包涵。

I'm pretty new to ASP.NET Web API, so please bear with me.

更新

控制器不包含特殊code。这是简单的code,它会从空阅读 API控制器生成/写操作模板。

The controller doesn't contain special code. It's the simple code, which gets generated from the API Controller with empty read/write actions template.

推荐答案

ASP.NET的WebAPI带有内置的<一个href=\"http://blogs.msdn.com/b/kiranchalla/archive/2012/02/25/content-negotiation-in-asp-net-mvc4-web-api-beta-part-1.aspx\">content negotitation 因此返回值的格式由请求本身决定的 - 更具体由接受 / 内容类型头(取决于哪些是present,接受标题出现在内容类型受到青睐)。

ASP.NET WebAPI comes with built-in content negotitation therefore the format of the return value is determined by the request itself - more specifically by the Accept/Content-Type headers (depending on which ones are present, Accept header appears to be favoured over the Content-type).

我假设你正在查看在浏览器中的结果,默认情况下它可能要求应用程序/ XML 。您将需要使用浏览器上的一些设置/开发工具玩具周围,并迫使它来发送内容类型:应用程序/ JSON 来得到正确的响应(假设你的回归<一个href=\"http://msdn.microsoft.com/en-us/library/system.net.http.htt$p$psponsemessage.aspx\">Htt$p$psponseMessage).

I assume you're viewing the results in a browser and by default it's probably asking for application/xml. You will need to toy around with some settings/developer tools on the browser and force it to send Content-Type: application/json to get the correct response (assuming your returning HttpResponseMessage).

这篇关于如何设置的ASP.NET Web API默认的序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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