简体   繁体   English

WCF REST服务为POST返回405

[英]WCF REST service returns 405 for POST

GET works, but when POST is invoked my service responds with a 405 method not allowed. GET有效,但是当调用POST时,我的服务以不允许的405方法响应。

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

and my methods (Get options tried after 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
    }

I have also added handlers to my web config file 我还在我的web配置文件中添加了处理程序

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

I can't change the Uri to use different for each method. 我不能改变Uri使用不同的方法。 I use get for validation and post to receive data. 我使用get进行验证并发布以接收数据。 Wireshark showed this 405 error. Wireshark显示405错误。

You need a * in UriTemplate. 你需要在UriTemplate中使用*。 I was able to see issue when there is no *. 当没有*时,我能够看到问题。

UriTemplate = "" //Wrong UriTemplate =“”//错了

UriTemplate = "*" /Working UriTemplate =“*”/工作

Is the POST working when invoked from same domain? 从同一域调用POST是否有效?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM