一、ConfigureServices中注册服务
var corsArray = configuration["AllowedHosts:MyCors"].Split(','); services.AddCors(option => { option.AddPolicy(name: "MyCors", policy => { policy.AllowAnyHeader() .AllowAnyMethod() .AllowCredentials() .AllowAnyOrigin() .WithOrigins(corsArray); }); } );
二、appsettings.json中进行配置
"AllowedHosts": { "MyCors": "http://localhost:7512,http://22.11.111.111:8888,http://localhost:18010" },