Why can't I make cross origin requests to an API hosted in IIS?

一世执手 提交于 2020-01-24 20:12:29

问题


I have an ASP.NET WebApi application running locally using IISExpress that allows me to accept requests from any domain. I am doing this using a DelegatingHandler similar to the one provided one this blob post.

Locally this runs perfectly however after uploading to an Azure Website, I get the typical 'Origin http://localhost:8080 is not allowed by Access-Control-Allow-Origin.' under Chrome.

I've tried to debug this by adding trace statements into the Handler with no output and it seems like SendAsync is never being executed, almost as if IIS is responding to the OPTIONS request instead of passing it on to my application.

Has anyone come across anything similar going from development to production?


回答1:


IIS (including the one in your Azure web site) has a default OPTIONS handler. You will need to remove it in Web.config. It answers the OPTIONS call before your message handler has an opportunity to respond.

<configuration>
  ...
  <system.webServer>
    <handlers>
      <remove name="OPTIONSVerbHandler" />
      ...
    </handlers>
  </system.webServer>
</configuration>


来源:https://stackoverflow.com/questions/17233632/why-cant-i-make-cross-origin-requests-to-an-api-hosted-in-iis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!