内容简介:下面小编就为大家分享一篇asp.net core webapi 服务端配置跨域的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
在前后端分离开发中服务端仅仅只为前端提供api接口,并且前后端往往单独部署,此时就会出现浏览器跨域问题。asp.net core提供了简单优雅的解决方案。
在startup文件的Configure添加如下代码(替换“http://localhost:8080”为你的前端部署地址,此处测试的前端地址为本地的8080端口)
注:asp.net core2.0以下需安装nuget包:Microsoft.AspNetCore.Cors
app.UseCors(builder =>
{
builder.AllowAnyHeader();
builder.AllowAnyMethod();
builder.WithOrigins("http://localhost:8080");
});
如果在开发环境只需替换builder.WithOrigins("http://localhost:8080")为builder.AllowAnyOrigins()即可允许任意的来源的地址跨域访问(不建议生产环境使用)
以上所述就是小编给大家介绍的《asp.net core webapi 服务端配置跨域的实例》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Nature of Code
Daniel Shiffman / The Nature of Code / 2012-12-13 / GBP 19.95
How can we capture the unpredictable evolutionary and emergent properties of nature in software? How can understanding the mathematical principles behind our physical world help us to create digital w......一起来看看 《The Nature of Code》 这本书的介绍吧!