- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我用 C# 创建了一个网络服务 (REST)。现在我希望当有人使用它时,它应该根据 header 返回 JSON 或 XML。我找到了一个很好的tutorial here .我跟着它,但我不知道它在哪里说 set both the HTTP Accept and Content-Type headers to "application/xml"
,我这样调用它 http://localhost: 38477/社交/姓名
。如果我的问题对你来说不是很清楚,我可以回答任何问题谢谢这是我的代码
[WebInvoke(UriTemplate = "{Name}", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
public MyclassData Get(string Name)
{
// Code to implement
return value;
}
最佳答案
您使用什么框架(看起来像旧的 WCf Web Api)来构建您的 RESTful 服务?我强烈推荐使用 Microsoft 的新 MVC4 Web API。它真正开始成熟并极大地简化了 RESTful 服务的构建。这是 WCF Web API 即将停用的 future 将要支持的内容。
您只需将 ModelClass 作为返回类型返回,它会根据请求接受 header 自动将其序列化为 XML 或 JSON。您可以避免编写重复代码,您的服务将支持范围广泛的客户。
public class TwitterController : ApiController
{
DataScrapperApi api = new DataScrapperApi();
TwitterAndKloutData data = api.GetTwitterAndKloutData(screenName);
return data;
}
public class TwitterAndKloutData
{
// implement properties here
}
链接
您可以通过仅下载 MVC4 2012 RC 获得 MVC4 Web Api,也可以下载整个 Visual Studio 2012 RC。
MVC 4:http://www.asp.net/mvc/mvc4
对比 2012:http://www.microsoft.com/visualstudio/11/en-us/downloads
对于原始的 wcf web api,请尝试一下。检查接受 header 并根据其值生成您的响应。
var context = WebOperationContext.Current
string accept = context.IncomingRequest.Accept;
System.ServiceModel.Chanells.Message message = null;
if (accept == "application/json")
message = context.CreateJsonResponse<TwitterAndCloutData>(data);
else if (accept == "text/xml")
message = context.CreateXmlResponse<TwitterAndCloutData>(data);
return message;
您可以在发起请求的任何客户端上设置接受 header 。这将根据您用于发送请求的客户端类型而有所不同,但任何 http 客户端都可以添加 header 。
WebClient client = new WebClient();
client.Headers.Add("accept", "text/xml");
client.DownloadString("domain.com/service");
要访问您将使用的响应 header
WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
其他资源:http://dotnet.dzone.com/articles/wcf-rest-xml-json-or-both
关于c# - 在 C# 中将 HTTP Accept 和 Content-Type header 都设置为 "application/xml",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11385031/
在 HTTP 中,您可以在请求中指定您的客户端可以使用 accept header 接受响应中的特定内容,其值如 application/xml。内容类型规范允许您在内容类型中包含参数,例如chars
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 Improve th
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 3 年前。 Improve th
我的 RESTFul API 只能响应 JSON 编码的数据(即我所有的 header 都有 Content-Type: application/json)。如果请求具有不允许 JSON 的 Acce
我有 C linux TCP 客户端/服务器应用程序。我想出了一个奇怪的场景,但我不知道这个应用程序是否有任何后果。我有一个可以接受 N 个连接的服务器端,例如这个服务器将接受 100 个连接。在这种
我试图找出 的可能值范围接受 和 接受-联系 header 字段,但我在 RFC 中找不到完整列表。有谁知道他们在哪里?我经常看到 Accept: application/sdp;level=1,
在 TCP 套接字代码中,我们创建了 2 个套接字。第一个接受新连接,第二个接受来自客户端的数据并在新连接建立时创建。 TCP header 中的哪个控制位允许服务器知道将此段传递给 ServerSo
我可以设置 Request.Content-Type = ... , Request.Content-Length = ... 如何设置Accept和Accept-Language? 我想上传一个文件
假设A是服务器,B是客户端。 B 创建一个到服务器 A 的套接字 b 并 ServerSocket.accept() 创建一个相应的套接字 a 对于客户端 B 现在,如果服务器 A 关闭了 a 但客户
如果客户端connect()先于服务端accept()成功,那么客户端如何知道服务端accept()成功呢?如果在 connect() 成功和服务器 accept() 之间的时间间隔调用 send()
这是我的代码: void error(const char *msg) { perror(msg); exit(1); } void sServer::acceptClientConn
在我的 pom.xml 中有以下详细信息 org.codehaus.jackson jackson-core-asl 1.9.13
假设我有两个如下所示的端点: @GET @Path("/blah") @Produces(MIME_TYPE_1) public Thing getThing() { .... } @GET
我的 Controller : @RestController public class ClawerController { @RequestMapping("/hello"
我不想要可视化 merge 工具,我也不希望必须 vi 冲突文件并手动在 HEAD(我的)和导入的更改(他们的)之间进行选择。大多数时候,我要么想要他们的所有更改,要么想要我的所有更改。通常这是因为我
我正在使用 spring 4.1.1.RELEASE 并包括: pom 中的 jackson-core-asl 1.9.13 和 jackson-mapper-asl 1.9.13 来创建一个带有 R
我正在尝试开发自己的基于非阻塞 NIO 消息的通信库。我已经阅读了 1000 个关于它的教程和书籍章节,我认为最终我得到了一些可以在很少的同时连接下工作的东西。但是当我在服务器端共存许多连接时,我遇到
我正在尝试编写一个 Bind 元编程模板辅助元函数,将模板参数绑定(bind)到某物。 我有一个简单的模板元函数的工作实现: template struct MakePair { using
我使用 spring 构建 Restful API,当我访问以下方法时: // get the entity in DB by using id number @RequestMapping(
只是想知道是否有一种方法可以通过 ASP.NET Core 中的属性强制检查 HTTP header 值是否属于一组给定值(例如 application/json,应用程序/xml) 我知道有 Pro
我是一名优秀的程序员,十分优秀!