在Startup.cs文件中 public IServiceProvider ConfigureServices(IServiceCollection services)中添加json
services.AddCors( options => options.AddPolicy( "AlloweDomain", builder => builder .WithOrigins( // App:CorsOrigins in appsettings.json can contain more than one address separated by comma. _appConfiguration["App:CorsOrigins"] .Split(",", StringSplitOptions.RemoveEmptyEntries) .Select(o => o.RemovePostFix("/")) .ToArray() ) .AllowAnyHeader() .AllowAnyMethod() .AllowCredentials() ) );
在容許跨域的Controller中添加。也能夠在ControllerBase中添加容許所有繼承繼承類的Controller。跨域
[EnableCors("AlloweDomain")] public class FileGetController : ServiceControllerBase { }
在appsettings.json添加容許訪問的域名。app
"App": { "CorsOrigins": "http://localhost:4200,http://localhost:8080,http://localhost:8081,https://www.baidu.com" },