2

我正在尝试将对象参数作为 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 调用服务时,服务返回“方法不允许”错误消息。

4

1 回答 1

21

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

要修复,要么更改Method = "GET"它是否对 REST 有意义,要么尝试从支持 POST 的工具(例如FiddlerWcfTestClient )调用服务方法

于 2012-12-27T08:31:03.723 回答