405方法不允许Wcf服务和离子

时间:2017-05-11 09:36:05

标签: c# wcf cors

我正在使用wcf rest api与离子应用程序进行通信。在使用rest api时,控制台引发了错误“405 Method not allowed”。通过搜索谷歌发现必须有“CORS错误”。

通过在Global.asax文件

中添加以下行重新配置Wcf服务
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>

现在我看到了

Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed. Origin 'http://localhost:8100' is therefore not allowed access.

任何帮助

1 个答案:

答案 0 :(得分:1)

我猜我现在的问题。请使用*中的[ServiceContract]。像这样:

[WebInvoke(Method = "*"

这样您就可以使用方法接收选项请求。