我正在处理基于 WCF 的 Rest 服务。我在服务中编写了 Get 和 Post 方法,并且当我在 URL 中键入(以 JSON 格式)时,Get 方法能够正常工作(获取数据)。
问题在于,当我尝试对 POST 方法执行相同操作时,URL 跳转到另一个页面 "页面未找到..."。
我了解到 POST 方法需要提交表单来处理请求。
因此,我尝试使用 Chrome 扩展程序(Simple Rest 客户端、Advanced Rest 客户端、Post man Rest 客户端)和 Fiddler。
下面是我发布的服务方法 - Get 方法(接口方法声明)。
这是我的POST方法。
这是我用于服务的web.config文件。
问题在于,当我尝试对 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 方法)是否有任何错误,或者测试它的方式是否正确。
我需要你们所有专家的帮助,我已经为这个问题奋斗了2天,最后我来这里发帖求助。
非常感谢任何帮助。