2015-02-24 132 views
0

GET工作,但是當POST被調用時,我的服務以不允許的405方法響應。WCF REST服務返回405用於POST

[ServiceContract] 
public interface IRestMeraki 
{ 
    [OperationContract] 
    [WebInvoke(Method = "OPTIONS", UriTemplate = "")] 
    void GetOptions(); 

    [OperationContract] 
    [WebGet(
    ResponseFormat = WebMessageFormat.Json, 
    BodyStyle = WebMessageBodyStyle.Bare, 
    UriTemplate = "json/")] 
    void JSONData(); 

    [OperationContract] 
    [WebInvoke(Method = "POST", 
    ResponseFormat = WebMessageFormat.Json, 
    BodyStyle = WebMessageBodyStyle.Bare, 
    UriTemplate = "json/{value}")] 
    void Post(string value); 
} 
} 

和我的方法(GET選項reading this後試過)

public void GetOptions() 
    {   
     WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*"); 
     WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Methods", "POST, GET, OPTIONS"); 
     WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Headers", "Content-Type"); 


    } 

     public void JSONData() 
    { 
     //my code here 
    } 


    public void Post(string value) 
    { 
//my code here 
    } 

我還添加了處理程序,我的網絡配置文件

<handlers> 
    <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> 
    <remove name="OPTIONSVerbHandler" /> 
    <remove name="TRACEVerbHandler" /> 
    <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*" verb="POST, GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> 
</handlers> 

我不能更改URI使用每種方法都不同。我使用get進行驗證併發布以接收數據。 Wireshark顯示了這個405錯誤。

回答

0

你需要在UriTemplate中*。當沒有*時,我能夠看到問題。

UriTemplate = 「」 //錯誤

UriTemplate = 「*」/工作

是後從同一個域調用時的工作?

+0

不,這不是。我甚至不記得我是如何工作的,因爲我嘗試了一切,並改變了網絡配置中的一切,並最終改變了我的服務合同。但因爲你是唯一回答的人。 – Redhead 2015-12-07 17:25:28