Startup.cs中
private readonly string AllowSpecificOrigin = "AllowSpecificOrigin";
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
#region 跨域
services.AddCors(options =>
{
options.AddPolicy(AllowSpecificOrigin,
builder =>
{
builder.WithOrigins("http://localhost:8080", "http://localhost:8082", "http://127.0.0.1:8080", "http://127.0.0.1")
.AllowAnyMethod()
.AllowAnyOrigin()
.AllowAnyHeader();
//.AllowCredentials()
;
});
});
#endregion
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
#region 跨域
//CORS 中间件必须配置为在对 UseRouting 和 UseEndpoints的调用之间执行。 配置不正确将导致中间件停止正常运行。
app.UseCors(AllowSpecificOrigin);
#endregion
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}