且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法使CORS适用于ASP.NET Core Web API

更新时间:2021-08-05 22:25:58

您可以尝试在services.UseCors()调用中而不是services.AddCors()上直接配置CORS选项。

You can try to configure CORS options right in services.UseCors() call, rather than services.AddCors().

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


public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseCors(builder => builder
        .WithOrigins("http://localhost:8000")
        .AllowAnyHeader()
        .AllowAnyMethod()
    );

    app.UseMvcCore();
    ...
}




注意:通过清除客户端缓存(如果有)对其进行测试。在Chrome> F12>网络选项卡>勾选禁用缓存复选框>刷新页面。