【问题标题】:wcf service returns "method not allowed" errorwcf 服务返回“方法不允许”错误
【发布时间】:2012-12-27 07:48:32
【问题描述】:

我正在尝试将对象参数作为 json 格式传递给 wcf restful 服务。

这样的服务合约代码;

[WebInvoke(
    Method = "POST",
    BodyStyle = WebMessageBodyStyle.Wrapped,
    ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json,
    UriTemplate="PR")]
[OperationContract]
TWorkRequestPostResult PostRequest(TWorkRequestPostArgs args);

我的 web.config 文件是这样的;

<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false 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>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="WebDAVModule"/>
    </modules>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true" />
  </system.webServer>  

当我尝试使用“http://localhost/serviceurl/PR”url 调用服务时,服务返回“方法不允许”错误消息。

【问题讨论】:

    标签: wcf rest


    【解决方案1】:

    您是从浏览器调用服务吗?如果是这样,浏览器使用 HTTP GET 请求服务,而服务方法映射到 HTTP POST,Method = "POST",从而导致错误"Method not allowed"。

    要修复,如果对 REST 有意义,请更改为 Method = "GET",或者尝试从支持 POST 的工具调用服务方法,例如Fiddler 或 WcfTestClient

    【讨论】:

    • 感谢您的帮助,您的解决方案解决了我的问题。我用过提琴手。
    猜你喜欢
    • 2010-09-07
    • 2017-10-13
    • 2016-09-20
    • 1970-01-01
    • 2018-07-21
    • 2012-04-21
    • 2011-03-04
    • 1970-01-01
    相关资源
    最近更新 更多