wcf service returns “method not allowed” error

2019-04-02 06:14发布

I'm trying to pass object parameter as json format to wcf restful service.

service conratc code like this;

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

And my web.config file like this;

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

When I trying call the service with "http://localhost/serviceurl/PR" url, Service returned "Method not allowed" error message.

标签: wcf rest
1条回答
smile是对你的礼貌
2楼-- · 2019-04-02 06:34

Are you calling the service from the browser? If so, the browser requests the service using HTTP GET while the service method is mapped to HTTP POST, Method = "POST", thus resulting in the error "Method not allowed".

To fix, either change to Method = "GET" if it makes sense with regards to REST or try calling the service method from a tool that supports POST, e.g. Fiddler or WcfTestClient

查看更多
登录 后发表回答