在Startup类里面先配置ConfigureServices方法
#region 跨域访问 string[] coreslink = Configuration.GetSection("AppSetting:Cores").Value.Split(','); //services.AddCors(options => // { // options.AddPolicy("all", builder => // { // builder.WithOrigins(coreslink) //允许特定来源的主机访问 // .AllowAnyMethod() // .AllowAnyHeader() // .AllowCredentials(); // }); // }); services.AddCors(options => options.AddPolicy("all", builder => { builder.AllowAnyMethod()//允许任何来源的主机访问 .SetIsOriginAllowed(_ => true) .AllowAnyHeader() .AllowCredentials(); }) ); #endregion 跨域访问
builder.WithOrigins(coreslink) 是配置允许特定来源的主机访问
其次在Configure方法配置,跨域访问要放在
UseRouting和UseAuthorization之间
app.UseRouting(); #region 跨域访问 app.UseCors("all"); #endregion 跨域访问 app.UseAuthorization();