【后端】Asp.Net Core 2.1设置跨域问题

120 阅读1分钟

如果当前请求站点域名是wwww.aaa.com,而目标接口的域名地址是www.bbb.com,那么在core框架默认情况下未开启跨域,请求时就会提示错误

1、跨域错误提示

image.png

2、后台解决方法

在Startup.cs文件,在如下方法添加代码

public IServiceProvider ConfigureServices(IServiceCollection services)
{
   services.AddCors();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
  app.UseCors(builder => builder
                .AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader()
                .AllowCredentials());
}

3、访问通过效果

image.png