使用wcf webinvoke post时方法不允许出错

时间:2011-05-30 07:51:27

标签: wcf

以下是操作合同,即插入一些数据,所以我使用WebInvoke POST方法,但是当我调用这个方法时,它给我一个错误,说“方法不允许”

我是否必须更改任何配置设置以允许在web.config中进行POST调用?

 [OperationContract]
 [WebInvoke(
 UriTemplate = "/Album/PostData?name={name}&CrBy={createdBy}" , 
 Method="POST")]
 void PostUserData(string name, string createdBy);

我正在调用我的服务,如下所示

http://localhost:2170/MySampleService.svc/xml/Album/PostData?name=devpost&CrBy=postadmin

4 个答案:

答案 0 :(得分:2)

如果你想这样打电话给你的服务,你应该使用Method="GET"。此外,方法名称和参数的定义似乎与您的查询字符串不匹配。

如果你想使用POST动词,那么你需要发送一个POST请求,你将无法通过在浏览器中直接输入url来调用该服务。

答案 1 :(得分:1)

您需要添加web.config

1

<endpoint address="customBinding" binding="customBinding" bindingConfiguration="basicConfig" contract="WcfRest.IService1"/>

2

<bindings>
      <customBinding>
        <binding name="basicConfig">
          <binaryMessageEncoding/>
          <httpTransport transferMode="Streamed" maxReceivedMessageSize="67108864"/>
        </binding>
      </customBinding>
    </bindings>

答案 2 :(得分:0)

在界面中将您的方法更改为

public class InputClass
{


public string Name{get;set;}
public string CreatedBy{get;set}
}

    [OperationContract]
     [WebInvoke(
     UriTemplate = "PostUserData" , 
     Method="POST",
    BodyStyle = WebMessageBodyStyle.WrappedRequest,
    ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json))]
     void PostUserData(InputClass input);

请参阅该链接以获取更多信息。http://fszlin.dymetis.com/post/2010/05/10/Comsuming-WCF-Services-With-Android.aspx

答案 3 :(得分:0)

您是否在浏览器中使用javascript调用服务?

html页面是否与wcf服务位于同一个域中?

如果他们不在同一个域,那么我会说这是跨站点脚本问题。我相信GET是允许跨站点,但POST不是。 http://en.wikipedia.org/wiki/JSONP是一个解决方案,如果它支持服务器端(通过WCF)