C# Web API 由于默认对 OPTION 请求的不一样的处理,导致部分会通过 OPTION 请求来检测是否可跨域的浏览器,在跨域时总是还没开始就被拒绝。
处理办法
在 Web.config 中配置跨域,修改默认配置。
重点先行,注释掉以下这句
<!--<remove name="OPTIONSVerbHandler" />-->
相关配置结果
<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<!--<remove name="OPTIONSVerbHandler" />-->
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept, WG-App-Version, WG-Device-Id, WG-Network-Type, WG-Vendor, WG-OS-Type, WG-OS-Version, WG-Device-Model, WG-CPU, WG-Sid, WG-App-Id, WG-Token, token" />
<add name="Access-Control-Allow-Methods" value="POST, GET, OPTIONS, PUT, DELETE" />
<add name="Access-Control-Allow-Credentials" value="false" />
</customHeaders>
</httpProtocol>
</system.webServer>
网友评论