使用带有POST的WCF REST

时间:2010-12-05 15:13:15

标签: wcf json post

我有一个WCF服务,主要使用GET,但一个合同应该与POST一起使用。 我不能让它工作 - 它一直返回“405 Method Not Allowed”。

服务应该接收JSON并返回JSON。

我想这与配置有关。这是我的web.config文件:

<behaviors>
  <serviceBehaviors>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>

<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
  multipleSiteBindingsEnabled="true" />
<standardEndpoints>
  <webHttpEndpoint>
     <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat="Json" />
  </webHttpEndpoint>
</standardEndpoints>

<behaviors> <serviceBehaviors> <behavior name=""> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> <standardEndpoints> <webHttpEndpoint> <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat="Json" /> </webHttpEndpoint> </standardEndpoints>

和服务本身

    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "LoginUser", BodyStyle=WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat=WebMessageFormat.Json)]
    public int Login(string user, string password)

{ .... }

有什么想法吗?非常感谢帮助!

2 个答案:

答案 0 :(得分:0)

确保你真的在发帖子。因为您的代码似乎有效。

通过创建包含以下内容的新html文件来尝试:

<form action="http://{Address to your service}/Service.svc/LoginUser" method="POST">
    <input type="submit" value="Fire away!" />
</form>

答案 1 :(得分:0)

查看我对重复项的评论,但归结为确保您在操作合同中提供了正确的动词。这是一个例子:

[ServiceContract(Namespace = "http://www.test.com/youruri")]
public interface ISomeService
{
    [OperationContract]
    [WebInvoke(Method = "POST")]
    string SomeMethod();
}