问题描述
我工作的一个WCF基于REST的服务。我已经写了GET和POST方法在我的服务和Get方法能够工作(读取数据),当我在URL输入(JSON格式。
I am working on a WCF rest based services. I have written Get and Post methods in my service and Get methods are able to working (fetching data) when I typed in URL (in JSON format.
是问题,当我试图为POST方法,网址导航被其他一些网页找不到网页......这样做。
Problem is when I try to do the same for POST methods, the url was navigating to some other page "Page not found...".
我了解,POST方法需要一种形式提交处理请求。
I understood that a POST method requires a form submission to process the request.
有关的原因,我曾尝试Chrome扩展(简单的REST客户端,高级REST客户端,邮人休息客户端)和提琴手也。
For that the reason, I have tried chrome extensions (Simple Rest client, Advanced Rest client, Post man rest client) and Fiddler also.
下面我张贴我的服务方法 - GET方法(接口方法声明)
Here I am posting my service method - Get method (interface method declaration).
[OperationContract]
[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "GetCategoryTypes/")]
List<CategoryType> GetCategoryTypes();
这就是我的POST方法
and this is my POST method
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "AddOrders/",
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest)]
int AddOrders(decimal amount, int tableID, DateTime orderDate, int isActive);
下面是服务我的web.config文件。
Here is my web.config file for the service.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="ServiceBehaviour" allowCookies="true" messageEncoding="Mtom" />
</basicHttpBinding>
<webHttpBinding>
<binding name="ServiceBehaviour1" allowCookies="true"/>
</webHttpBinding>
</bindings>
<services>
<service name="EMC.DD.ServiceLayer.Service1" ehaviorConfiguration="ServiceBehaviour">
<endpoint address="http://localhost/EMCService/Service1.svc"
binding="basicHttpBinding" contract="EMC.DD.ServiceLayer.IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</service>
<service name="EMC.DD.ServiceLayer.Service2"
behaviorConfiguration="ServiceBehaviour1">
<endpoint address="http://localhost/EMCService/Service2.svc"
binding="webHttpBinding" behaviorConfiguration ="web"
contract="EMC.DD.ServiceLayer.IService2">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name ="ServiceBehaviour">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
<behavior name ="ServiceBehaviour1">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name ="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
<system.webServer>
<directoryBrowse enabled="true" />
</system.webServer>
</configuration>
我真的不知道是否有我的方法有错误构造(POST方法),否则这样,我需要测试它。
I am not really sure whether there is any mistake in my method constructing (POST method) or else the way I need to test it.
我需要所有的专家的帮助下,我这个问题争取在过去的2天,atlast我来到这里后吧。
I need an help from all of your experts and I am fighting with this issue for the past 2 days and atlast I came here to post it.
任何帮助高度赞赏。
推荐答案
最后经过很多试验中,很多事情,我得到了工作方案。在这里,我张贴我所做的POST方法来工作。
Finally after many trials and many things, I got the working solution. Here I am posting what I did for the POST method to work.
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "AddOrders", RequestFormat =
WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,BodyStyle =
MessageBodyStyle.Bare)]
int AddOrders(RequestData orderRequestData);
这是在经营合同执行。
客户端应用程序: -
Client application:-
WebClient WC = new WebClient();
WC.Headers.Add("Content-Type", "application/json");
WC.Encoding = Encoding.UTF8;
MemoryStream MS = new MemoryStream();
DataContractJsonSerializer JSrz = new
DataContractJsonSerializer(typeof(RequestData));
JSrz.WriteObject(MS, order);
string data = Encoding.UTF8.GetString(MS.ToArray(), 0, (int)MS.Length);
byte[] res1 =
WC.UploadData("http://localhost/EMCService/Service2.svc/AddOrders", "POST",MS.ToArray());
MS = new MemoryStream(res1);
JSrz = new DataContractJsonSerializer(typeof(int));
int result = (int)JSrz.ReadObject(MS);
我并没有作出任何的配置设置,并仍然使用我所张贴的web.config的旧的设置在上面的问题,这是工作。
I did not made any config settings and still using the old settings of web.config what I have posted in the above question, and it is working.
这篇关于如何参数(S)传递给WCF POST方法(一个RESTful服务)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!