如何将参数传递给WCF post方法(restful服务)

本文关键字:方法 restful 服务 post WCF 参数传递 | 更新日期: 2023-09-27 18:21:51

我正在开发一个基于WCF rest的服务。我已经在我的服务中编写了Get和Post方法,当我输入URL(JSON格式.)时,Get方法能够工作(获取数据)

问题是,当我尝试对POST方法进行同样的操作时,url导航到其他页面"找不到页面…"。

我知道POST方法需要提交表单来处理请求。

因此,我尝试了chrome扩展(Simple Rest客户端、Advanced Rest客户端和Post man Rest客户端)和Fiddler。

在这里,我发布了我的服务方法-Get方法(接口方法声明)。

[OperationContract]
[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, 
ResponseFormat =   WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, 
UriTemplate = "GetCategoryTypes/")]
List<CategoryType> GetCategoryTypes();

这是我的POST方法

[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文件。

<?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方法)或我需要测试它的方式是否有任何错误

我需要你们所有专家的帮助,在过去的两天里,我一直在与这个问题作斗争,至少我是来这里发布它的。

非常感谢您的帮助。

如何将参数传递给WCF post方法(restful服务)

经过多次尝试和许多事情,我终于得到了可行的解决方案。在这里,我发布了我为POST方法工作所做的工作。

  [OperationContract]
  [WebInvoke(Method = "POST", UriTemplate = "AddOrders", RequestFormat =   
  WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,BodyStyle = 
  MessageBodyStyle.Bare)]
  int AddOrders(RequestData orderRequestData);

这是为了在运营合同中执行。

客户端应用程序:-

        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的旧设置,它正在工作。