2011-03-30 96 views
1

我試圖通過創建一個WCF REST服務,並已成功實施了經[WebInvoke(Method = "GET")]問題:在.NET

調用的函數REST服務現在我想創建一個使用Method="POST"更新功能。這會導致405:方法不允許。我懷疑我可能需要在我的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 req此方法Method = "POST"我得到一個錯誤405:不允許的方法。

我的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> 
+0

你也發送了什麼URL? – SecretDeveloper 2011-03-30 08:45:33

+0

我使用的URL是:GET請求的http:// localhost:54270/Service1.svc/object/1和POST請求的http:// localhost:54270/Service1.svc/object。 – 2011-03-30 09:00:41

+0

@Secret開發人員:謝謝。你的問題引發了另一個新的面貌,現在我發現了這個問題。 – 2011-03-30 10:35:08

回答

0

我發現問題的原因。我通過錯誤的URL訪問POST。我用../object而不是../object/1。

奇怪的是,當您進行POST時,您不會收到執行GET時得到的endpoint not found錯誤,但會出現method not allowed錯誤。

0

我認爲問題是,你只能在後的數據發送一個數據項,但您要發送2(狀態&原因)。

作爲測試,您可以刪除其中一個參數,並嘗試只發送一個項目。這至少會確認問題並從可能出現的問題中刪除您的web.config。