为什么我会收到“不允许使用405方法"的信息? “放置"错误通过WCF Resful服务进行操作? [英] Why am I getting a "405 Method not allowed" error on "Put" operations through a WCF Resful service?

查看:103
本文介绍了为什么我会收到“不允许使用405方法"的信息? “放置"错误通过WCF Resful服务进行操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的WCF服务,该服务将json作为输入/输出参数. 它可以作为基于SOAP的服务正常工作,并且可以得到预期的结果.

I've got a simple WCF service which takes json as input/output parameters. It works correctly as a SOAP based service and I get the expected results.

我已将RESTFul端点添加到该服务. 我正在使用基于Google的" PostMan来测试服务.

I have added RESTFul endpoints to the service. I'm using "Google-based" PostMan to test the service.

我可以在服务上成功调用"Get"方法并返回JSON结果:

 [OperationContract(IsOneWay = false)]
    [WebGet(UriTemplate = "/Test/{calcID}", 
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Bare)]
    CalcResults GetTestResults(string calcID);

当我尝试访问基于放置"的方法时,我的状态为: 405不允许的方法

When I attempt to access my "Put" based methods I get a status of: 405 Method Not Allowed

放置"方法的签名:

 [OperationContract(IsOneWay = false)]
    [WebInvoke(Method = "Post",
        UriTemplate = "/GetCalculation", 
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Bare)]
    CalcResults GetCalculation(CalcInput inputValues);

这是我在PostMan输出窗口中看到的输出:

<body>
   <div id="content">
      <p class="heading1">Service</p>
      <p>Method not allowed.</p>
   </div>
</body>

这是我的RESTFul接口端点的配置设置:

This is what my configuration settings look like for the endpoints for the RESTFul interface:

<system.serviceModel>
    <services>
      <service name="CalcServiceLibrary.CalcService">

        <endpoint address="regular" binding="wsHttpBinding" name="wsHTTPEndPoints"
          contract="CalcServiceLibrary.ICalcService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>

        <endpoint address="json" binding="webHttpBinding" name="wsWebHTTPEndPoint"
                  contract="CalcServiceLibrary.ICalcServiceRESTful"
                  behaviorConfiguration="RESTfulBehavior" bindingConfiguration="crossDomain">
            <identity>
              <dns value="localhost" />
            </identity>          
        </endpoint>
      </service>
    </services>

    <bindings>
      <webHttpBinding>
        <binding name="crossDomain" crossDomainScriptAccessEnabled="true" />
      </webHttpBinding>
    </bindings>

    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="True"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="RESTfulBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>      
    </behaviors>

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

我确定在我的配置定义中有些不完全正确. 我研究了好几篇文章,但未能提出解决方案.

I'm sure that there is something that is not quite correct in my configuration definitions. I've researched several posts, but have not been able to come up with the solution.

新说明: 我已将以下简单端点添加到我的服务中:

NEW NOTE:: I've added the following simple endpoint to my service:

[OperationContract]
[WebInvoke(
    Method = "POST",
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json)]
public string TestPost(string name)
{
    return "Hello " + name + "!";
}

我正在使用邮递员进行测试.这是我的邮递员屏幕的屏幕截图:

I'm testing using Postman. Here is a screen shot of my Postman screen:

这是我在输出中看到的:

This is what I see in the output:

这似乎必须是某种配置问题. 有人可以指出我做错了正确的方向吗?

This feels like it has to be some sort of configuration issue. Can someone point me in the right direction as to what I'm doing wrong?

推荐答案

看起来像您是在声明中指定post而不是put作为方法,也许更改为:

Looks like you're specifying post instead of put as the method in the declaration, perhaps change to:

[OperationContract(IsOneWay = false)]
   [WebInvoke(Method = "PUT",
       UriTemplate = "/GetCalculation", 
       RequestFormat = WebMessageFormat.Json,
       ResponseFormat = WebMessageFormat.Json,
       BodyStyle = WebMessageBodyStyle.Bare)]
   CalcResults GetCalculation(CalcInput inputValues);

这篇关于为什么我会收到“不允许使用405方法"的信息? “放置"错误通过WCF Resful服务进行操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆