1、配置跨域
#region 配置跨域
//配置跨域
builder.Services.AddCors(cor =>
{
var cors = configuration.GetSection("CorsUrls").GetChildren().Select(p => p.Value);
cor.AddPolicy("Cors", policy =>
{
policy.WithOrigins(cors.ToArray())
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});
#endregion2、启用跨域
app.UseCors("Cors");3、在appsetting.json中配置可跨域操作的ip或者域名
"CorsUrls": [
"http://localhost:3000",
"http://localhost:8080",
"http://localhost:5000",
"https://localhost:5001"
]
本文介绍了如何在ASP.NETCore应用中配置跨域策略。首先,通过`AddCors`服务添加跨域支持,并从appsettings.json读取允许的源。然后,使用`UseCors`启用配置的跨域政策。最后,展示了在配置文件中定义的可跨域的IP和域名示例。
1443

被折叠的 条评论
为什么被折叠?



