【问题标题】:Why am I getting a "405 Method not allowed" error on "Put" operations through a WCF Resful service?为什么我通过 WCF Resful 服务在“Put”操作中收到“405 Method not allowed”错误?
【发布时间】:2016-04-17 17:59:13
【问题描述】:

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

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

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

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

当我尝试访问基于“Put”的方法时,我得到以下状态: 405 方法不允许

“Put”方法的签名:

 [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 接口端点的配置设置:

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

我确定我的配置定义中有些地方不太正确。 我研究了几篇帖子,但未能提出解决方案。

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

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

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

这是我在输出中看到的:

这感觉一定是某种配置问题。 有人可以指出我做错了什么的正确方向吗?

【问题讨论】:

    标签: c# .net json wcf wcf-rest


    【解决方案1】:

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

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

    【讨论】:

    • IR:感谢您的快速反馈。我尝试将其更改为“Put”,并且在 Postman 中得到相同的结果。想法?
    • 自从我使用 WCF 以来已经有一段时间了,我通常只做 POST 而不是弄乱 PUT。确实有助于查看@Ian 建议的发送请求的代码。
    • IR:我无权访问发送请求的代码。我正在使用“PostMan”进行测试。正如我上面提到的,当我使用 Method="*" 时,它开始工作。不知道为什么,也不确定这是否是正确的方法。
    • 我想知道它是否区分大小写。我之前并没有真正考虑过,但我敢打赌这就是为什么 Method="*" 对你有用而 Method="Put" 没有。我将在我的答案中更正代码,因为我刚刚使用了相同的样式而没有考虑。检查旧代码和文档,它总是大写的。
    • IR:就是这样。它区分大小写。在我查看的所有文档等中,没有任何地方说它需要全部是大写字母。谢了!
    【解决方案2】:

    在进行故障排除时,我尝试做的第一件事就是从最基本的要素开始。我建议删除 UriTemplate = "/GetCalculation"BodyStyle = WebMessageBodyStyle.Bare) 属性参数。这就是我所拥有的:

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

    编辑: 我想我会看看 Postman,因为无论如何我都在做很多类似的工作。您可以发布您的 Postman 配置的屏幕截图吗?为了让 Postman 工作,我必须设置以下内容:

    类型设置为 POST(显然)。但我还必须将标题 Content-Type 设置为 application/json

    为了发送数据,您不能使用 URL 参数(URL 字段旁边的“参数”按钮)。这仅插入样式?name=bob,适用于GET,但不适用于POST。另外我无法将数据指定为 JSON,所以我必须以 JSON 格式输入 raw 数据:

    然后我得到一个有效的回复,Hello bob!

    【讨论】:

    • 伊恩:感谢您的反馈。我会回去玩最基本的东西。注意::当我替换 Method="*" 时,我能够让它工作。不知道为什么....
    • -“方法不允许”。我正在使用 Postman 进行测试。这是我的测试代码:localhost:61047/CalculatorService.svc/json/TestPost。我能想到的唯一值得一提的是,我的服务设置为以下模式:CalculatorServiceLibrary -- CalculatorService。想法?
    猜你喜欢
    • 1970-01-01
    • 2018-04-17
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    • 2011-03-31
    • 2018-06-29
    • 1970-01-01
    • 2016-04-12
    相关资源
    最近更新 更多