C# 405方法不允许Wcf服务和Ionic
我正在使用wcf rest api与ionic应用程序通信。在使用RESTAPI时,控制台引发错误“405方法不允许”。通过搜索谷歌发现一定有一个“CORS错误” 通过在Global.asax文件中添加以下行重新配置Wcf服务C# 405方法不允许Wcf服务和Ionic,c#,wcf,cors,C#,Wcf,Cors,我正在使用wcf rest api与ionic应用程序通信。在使用RESTAPI时,控制台引发错误“405方法不允许”。通过搜索谷歌发现一定有一个“CORS错误” 通过在Global.asax文件中添加以下行重新配置Wcf服务 protected void Application_BeginRequest(object sender, EventArgs e) { HttpContext.Current.Response.AddHeader("Acces
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}
然后我得到了
the 'access-control-allow-origin' header contains multiple values
我还尝试在web.config文件中添加一个条目,如下所示
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
<add name="Access-Control-Allow-Methods" value="GET, POST" />
<add name="Access-Control-Max-Age" value="1728000" />
</customHeaders>
</httpProtocol>
有什么帮助吗?我想我现在可以解决这个问题了。请在
[ServiceContract]
中使用*
。像这样:
[WebInvoke(Method = "*"
这样,您就可以允许您的方法接收选项请求请考虑阅读-
[WebInvoke(Method = "*"