-
Notifications
You must be signed in to change notification settings - Fork 22
.net core 允许跨域
L edited this page Apr 6, 2019
·
3 revisions
在Startup的ConfigureServices()中添加
services.AddCors();
在Startup的Configure()中添加app.UseCors(); 保证其在app.UseMvc();之前
app.UseCors(builder => builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
app.UseMvc();
豁免的规则写在前面,限制的规则写在后面
所以app.UseCors()需要写在前面
参考资料
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/startup?view=aspnetcore-2.1
https://stackoverflow.com/questions/44379560/how-to-enable-cors-in-asp-net-core-webapi
https://docs.microsoft.com/en-us/previous-versions/aspnet/web-frameworks/mt181143(v=vs.113)
以上是.net core(即mvc 6)的解决方案,mvc 5的解决方案如下:
https://social.technet.microsoft.com/wiki/contents/articles/33771.fix-to-no-access-control-allow-origin-header-is-present-or-working-with-cross-origin-request-in-asp-net-web-api.aspx