Asp.Net跨域

在var app = builder.Build();之前添加

string[] url = new[] { "localhost:3000" };//允许跨域访问的前端端口
builder.Services.AddCors(opt =>
{
    opt.AddDefaultPolicy(build => build.WithOrigins(url)
    .AllowAnyMethod(). //所有方法
    AllowAnyHeader(). //所有请求头
    AllowCredentials());//所有证书
});
var app = builder.Build();

在app.UseHttpsRedirection(); 之前启用中间件

//if (app.Environment.IsDevelopment())
//{
//    app.UseSwagger();
//    app.UseSwaggerUI();
//} 
app.UseCors();//启用Cores
//app.UseHttpsRedirection(); 

 

posted @ 2022-01-21 09:18  后跳  阅读(50)  评论(0编辑  收藏  举报