【问题标题】:WCF REST service returns 405 for POSTWCF REST 服务为 POST 返回 405
【发布时间】:2015-04-26 04:57:34
【问题描述】:

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

和我的方法(获取在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 进行验证并使用 post 来接收数据。 Wireshark 显示了这个 405 错误。

【问题讨论】:

    标签: c# web-services wcf web http-status-code-405


    【解决方案1】:

    您需要在 UriTemplate 中使用 *。没有 * 时我可以看到问题。

    UriTemplate = "" //错误

    UriTemplate = "*" /Working

    从同一个域调用 POST 是否有效?

    【讨论】:

    • 不,不是这样。我什至不记得我是如何让它工作的,因为我尝试了一切并更改了 Web 配置中的所有内容,最终也更改了我的服务合同。但因为你是唯一一个回答的人。
    猜你喜欢
    • 2011-12-27
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 2018-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多