简体   繁体   English

如何处理ASP.NET网页和IIS中托管的WCF服务之间的跨域请求?

[英]How to handle cross domain request between an asp.net web page and a WCF service hosted in IIS?

I have written a WCF web service which is being hosted in Windows Server 2012 through IIS-7. 我编写了WCF Web服务,该服务通过IIS-7托管在Windows Server 2012中。 The server's public IP is currently open (the firewall is disabled). 服务器的公共IP当前处于打开状态(防火墙已禁用)。 I can get the metadata and the wsdl document just fine with a browser, and I can add the service reference to Visual Studio through that IP. 我可以使用浏览器很好地获取元数据和wsdl文档,并且可以通过该IP向Visual Studio添加服务引用。 However, when my asp.net web page invokes a WCF service method, it gives me a 405 - Method Not Allowed error. 但是,当我的asp.net网页调用WCF服务方法时,它给我405-不允许使用方法错误。

I think the problem is with cross-domain posting. 我认为问题出在跨域发布。 I could be wrong, but that would make sense, since I only get errors when accessing the wcf service from an application. 我可能是错的,但这是有道理的,因为从应用程序访问wcf服务时,我只会收到错误。

How would I go about avoiding this issue? 我将如何避免这个问题?

Here is my Interface: 这是我的界面:

[ServiceContract]
public interface IDataPortalWCFService
{
    [OperationContract]
    string HelloWorldTest(string u, string p);

    [OperationContract]
    string HelloWorldTest2(string u, string p, string str);

    [OperationContract]
    string HelloWorldTest3(string u, string p);

    [OperationContract]
    DataTable GetBAQResults(string u, string p, string BAQName, DataTable BAQParameters);

    [OperationContract]
    string GetBAQResultsAsJSON(string u, string p, string BAQName, string BAQParameters);

}

Dealing with cross domain headers when hosting a WCF service in IIS should be essentially the same as any other type of web site being hosted in IIS. 在IIS中托管WCF服务时,处理跨域标头应与在IIS中托管的任何其他类型的网站基本相同。 The following question should help you find a solution... 以下问题应可帮助您找到解决方案...

CORS settings for IIS 7.5 IIS 7.5的CORS设置

Another way of solving this issue would be to simply reuse the domain. 解决此问题的另一种方法是简单地重用域。

eg for your web site use:- 例如供您的网站使用:-

www.mygreat.com www.mygreat.com

and for your api use 和供您使用的api

api.mygreat.com api.mygreat.com

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

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