使用 WebInvoke POST 的 C# Restful 服务 [英] C# Restful Service Using WebInvoke POST

查看:91
本文介绍了使用 WebInvoke POST 的 C# Restful 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 C# 开发一个 Restful 服务,并且在我使用时运行良好

I am developing a Restful service in C# and working well when I use

[OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, BodyStyle =     
WebMessageBodyStyle.Wrapped, UriTemplate = "json/?id={id}")]
    string jdata(string id);

我对应的函数实现是:

public string json(string id)
{
 return "You Typed : "+id;
}

到这里一切正常,但是当我更改 WenInvoke Method="POST" 时,我必须面对不允许使用的方法.";

Up to here all works well, but when I change WenInvoke Method="POST" I have to face a "Method NOT Allowed.";

[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Xml, BodyStyle = 
WebMessageBodyStyle.Wrapped, UriTemplate = "json/?id={id}")]
    string jdata(string id);

推荐答案

您得到Method not allowed",因为您通过 GET 而不是 POST 到达 Urijson/?id={id}".请与您的客户核实这一点(您没有提到您如何调用此资源).请进一步详细说明您如何尝试在客户端使用 Web 服务.是 .Net 客户端吗?

You get "Method not allowed" because you are reaching the Uri "json/?id={id}" via GET instead of POST. Check this with your client (you didn't mention how you call this resource). Please give some further details how you are trying to use your web service in client. Is is .Net client?

要测试您的 API,我建议使用 Fiddler - 当您可以在发送 http 请求之前明确指定是使用 GET 还是 POST 时:

To test your API I recommend using Fiddler - when you can explicitly specify whether to use GET or POST before sending an http request:

另一件事是,您可能无意中将json"用作 Uri,但将 ResponseFormat 定义为 WebMessageFormat.Xml.对客户来说是不是有点混乱?也许你想返回 JSON?在这种情况下,我建议在请求和响应中都使用 Json:

Another thing is, you might have unwittingly used "json" as Uri, but defined ResponseFormat as WebMessageFormat.Xml. Isn't it a little confusing for the client? Maybe you wanted to return JSON back? In that case, I would recommend using Json in both - request and response:

[WebInvoke(Method = "POST", UriTemplate = "/ValidateUser", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]

这篇关于使用 WebInvoke POST 的 C# Restful 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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