【问题标题】:Problem with REST service in .NET.NET 中 REST 服务的问题
【发布时间】:2011-03-30 18:33:10
【问题描述】:

我正在尝试通过 wcf 创建 REST 服务并已成功实现通过 [WebInvoke(Method = "GET")] 调用的功能

现在我想创建一个使用Method="POST" 的更新函数。这失败了 405: Method not allowed。我怀疑我可能需要在我的 web.config 中配置一些东西。

我在 VS2010 调试器中运行我的 wcf 服务时收到此错误。

这是服务的定义:

[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare,
    UriTemplate = "object/{id}?status={status}&reason={reason}")]
Textblock SetObjectStatus(string id, string status, string reason);

当我通过 HttpWebRequest reqMethod = "POST" 调用此方法时,我收到错误 405: Method not allowed。

我的 web.config 如下所示:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="ServiceBehaviour" name="WcfService1.TextblockService">
        <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" contract="WcfService1.ITextblockService" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

【问题讨论】:

标签: .net rest post wcf


【解决方案1】:

我认为问题在于您只能在发布数据中发送一个数据项,但您发送的是 2 个(状态和原因)。

作为测试,您可以删除其中一个参数并尝试仅发送一项。这至少可以确认问题并将您的 web.config 从可能的问题中删除。

【讨论】:

    【解决方案2】:

    我找到了问题的原因。我通过错误的 URL 访问 POST。我使用 ../object,而不是 ../object/1。

    奇怪的是,当您执行 POST 时,您不会收到您执行 GET 时遇到的 endpoint not found 错误,但您会收到 method not allowed 错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-09
      • 1970-01-01
      相关资源
      最近更新 更多