一、在api配置文件Startup中定义全局变量:
public readonly string anyAllowSpecificOrigins = "any";
二、在api的appsettings.json中添加配置信息(允许跨域访问的url):
"CorsPaths": {
"OriginOne": "http://localhost:5001",
"OriginOnes": "https://localhost:5001",
"OriginThree": "http://www.hrms.com:80/",
"OriginThrees": "https://www.hrms.com:80/"
}
三、NuGet包管理器,安装:Microsoft.AspNetCore.Mvc.Cors
四、在api配置文件Startup中ConfigureServices方法里配置跨域处理cors:
//配置跨域处理cors
services.AddCors(options =>
{
options.AddPolicy(anyAllowSpecificOrigins, corsbuilder =>
{
var corsPath = Configuration.GetSection("CorsPaths").GetChildren().Select(p=>p.Value).ToArray();