vs2022 搭建NET6 WebApi 接口项目《三》 配置跨域

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();
    });
});
#endregion

2、启用跨域

     

app.UseCors("Cors");

3、在appsetting.json中配置可跨域操作的ip或者域名

     

  "CorsUrls": [
    "http://localhost:3000",
    "http://localhost:8080",
    "http://localhost:5000",
    "https://localhost:5001"
  ]

 

posted @ 2022-04-08 23:38  程序原快递  阅读(1106)  评论(0编辑  收藏  举报