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"
  ]

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM