开发者

Problem with REST service in .NET

I'm trying to create a REST service through wcf and have succesfully implemented functions that are called via [WebInvoke(Method = "GET")]

Now I want to create an update function that uses Method="POST". This fails with a 405: Method not allowed. I suspect that I probably need to configure something in my web.config.

I get this error when I run my wcf service in the VS2010 debugger.

This is the definition of the service:

[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);

When I call this method via a HttpWebRequest req with Method = "POST" I get a error 405: Method not allowed.

My web.config looks like this:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <co开发者_StackOverflow中文版mpilation 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>


I think the problem is that you can only send one data item in the post data but you are sending 2 (status & reason).

As a test can you remove one of the parameters and try sending only one item. This will at least confirm the problem and remove your web.config from the possible problems.


I found the cause of the problem. I was accessing the POST via the wrong URL. I used ../object, instead of ../object/1.

What is strange is that when you do a POST you do not get an endpoint not found error that you get when you do a GET, but you get a method not allowed error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜