【发布时间】: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