打赏

.net webapi项目跨域问题及解决方案

问题:

1.项目完成,部署到不同的iis版本上,跨域访问有的通有的不通

解决办法:

1.将复杂请求改为简单请求

2.代码中去掉所有跨域设置,配置中添加或修改节点

<system.webServer>
<modules>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
</modules>
<validation validateIntegratedModeConfiguration="false" />
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="POST,GET" />
<add name="Access-Control-Allow-Headers" value="x-requested-with,content-type,authorization,mypara,username" />
</customHeaders>
</httpProtocol>
</system.webServer>

 

 

<system.webServer>
<modules>
<remove name="FormsAuthentication" />
<remove name="ApplicationInsightsWebTracking" />
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
</modules>
<validation validateIntegratedModeConfiguration="false" />
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="OPTIONS,GET,POST,PUT,DELETE" />
<add name="Access-Control-Allow-Headers" value="x-requested-with,content-type,authorization,mypara,username" />
</customHeaders>
</httpProtocol>
</system.webServer>

 

 

 

 

 

<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
<add name="HttpHealthCheck" path="health.check" type="ZJCX.WebAPI.HealthCheckHandler,ZJCX.WebAPI" verb="GET" />
</handlers>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://123.56.26.99:9052" />
<add name="Access-Control-Allow-Credentials" value="true" />
<add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept,userid,corpid,errcode,errmsg" />
<add name="Access-Control-Expose-Headers" value="errcode,errmsg" />
</customHeaders>
</httpProtocol>
</system.webServer>

 

 

 

架构师资料:加博主QQ:552343492

posted @ 2017-10-31 09:32  刘奇云  阅读(723)  评论(0编辑  收藏  举报