【问题标题】:method not allowed error while using wcf webinvoke post使用 wcf webinvoke post 时方法不允许错误
【发布时间】:2011-09-04 15:23:49
【问题描述】:

您好,下面是操作合同,我想插入一些数据,所以我使用 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

【问题讨论】:

    标签: wcf


    【解决方案1】:

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

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

    【讨论】:

    • 不!获取操作不应修改数据。它们应该是幂等的。
    • @Ladislav Mrnka,没错,我没有注意到他正在尝试修改数据。
    • "而且您将无法通过直接在浏览器中输入 url 来调用服务" - 这为我节省了很多时间!!!谢谢!!!
    【解决方案2】:

    将界面中的方法更改为

    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】:

      您是在浏览器中从 javascript 调用您的服务吗?

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

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

      【讨论】:

        【解决方案4】:

        你需要在 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>
        

        【讨论】:

          猜你喜欢
          • 2012-07-10
          • 2013-09-06
          • 1970-01-01
          • 1970-01-01
          • 2018-02-16
          • 1970-01-01
          • 1970-01-01
          • 2012-10-25
          • 2012-08-24
          相关资源
          最近更新 更多