【发布时间】:2017-03-24 14:36:43
【问题描述】:
我有一个 MVC Web API(托管在 IIS 中),它位于 wwwroot 文件夹中,可以在网络中本地访问。我可以执行这样的 api 调用:http://mylocalapi:133/api/Values/Get 并得到结果。
我有一个外部站点 http://example.org,我想执行相同的 http://mylocalapi:133/api/Values/Get。
面向外部的站点和内部 API 站点都托管在同一台服务器上(但可以不同,例如网络外的第 3 方供应商)
我在我的 API 中设置了这样的 CORS:
[EnableCors(origins: "http://example.org", headers: "*", methods: "*")]
但我不断收到以下错误:
XMLHttpRequest cannot load http://mylocalapi:133. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://example.org' is therefore not allowed access.
所以绕过它,我在我的外部站点中创建了一个虚拟目录 (APICALLS) 并创建了一个指向本地 IIS 站点的 web.config 文件:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<httpRedirect enabled="true" destination="http://mylocalapi:133" exactDestination="true" />
</system.webServer>
</configuration>
当我这样做时,我尝试像这样访问 API:http://example.org/APICALLS/api/Values/Get 但我收到以下错误:
XMLHttpRequest cannot load http://mylocalapi:133. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://example.org' is therefore not allowed access.
我做错了什么以及如何解决问题。
【问题讨论】:
-
您正试图从外部站点向您的本地主机发出 Web 请求,我错了吗?如果是,则不能。
-
@ibubi 即使是虚拟目录?
标签: asp.net asp.net-mvc api cors