IIS & IIS Express已开启CORS,接收OPTIONS Preflight Request仍然报错

1014

跨域请求

即使后台开启了CORS跨域请求,接收预检请求时触发此错误
文件上传时会出现有时候会出现预检请求

            response.AddHeader("Access-Control-Allow-Origin", "PROTOCAL://IP:PORT");
            response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
            response.AddHeader("Access-Control-Allow-Credentials", "true");
            response.AddHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");

原因

IIS或者IIS Express拦截了预检请求

解决方案

webconfig中添加:

  <system.webServer>
    <handlers>
      <remove name="SimpleHandlerFactory-Integrated-4.0" />
      <add name="SimpleHandlerFactory-Integrated-4.0" path="*.ashx" verb="GET,HEAD,POST,DEBUG,OPTIONS" type="System.Web.UI.SimpleHandlerFactory" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>

当然,你可以在IIS的控制台找到对应网站,点击处理程序映射,进行可视化编辑,它同样会把上面的内容写入webconfig

image.png