ASP.NET Core 启用跨域CORS请求

  • A+
所属分类:.NET技术
摘要

在ConfigureServices方法中添加在Configure方法中添加https://docs.microsoft.com/zh-cn/aspnet/core/security/cors?view=aspnetcore-5.0#cors-with-named-policy-and-middleware


启用跨域 允许所有来源

修改Startup文件

在ConfigureServices方法中添加

services.AddCors(     options => options.AddPolicy(name: "MyAllowSpecificOrigins", builder => builder.AllowAnyOrigin())     ); 

在Configure方法中添加

app.UseCors("MyAllowSpecificOrigins"); 

官方文档

https://docs.microsoft.com/zh-cn/aspnet/core/security/cors?view=aspnetcore-5.0#cors-with-named-policy-and-middleware