為所有請求啟用 CORS

Configure 方法中使用 IApplicationBuilder 上的 UseCors() 擴充套件方法將 CORS 策略應用於所有請求。

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.AddCors();
}

public void Configure(IApplicationBuilder app)
{
    // Other middleware..

    app.UseCors(builder =>
    {
        builder.AllowAnyOrigin()
               .AllowAnyHeader()
               .AllowAnyMethod();
    });

    // Other middleware..

    app.UseMvc();
}