在NGINX中,需要在配置文件中添加以下指令:
location / { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST'; add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type'; if ($request_method = 'OPTIONS') { add_header 'Access-Control-Max-Age' 1728000; add_header 'Content-Type' 'text/plain charset=UTF-8'; add_header 'Content-Length' 0; return 204; } }
在ASP.NET Core中,可以使用Microsoft.AspNetCore.Cors包来实现CORS策略。首先,在Startup.cs文件中添加以下代码:
public void ConfigureServices(IServiceCollection services) { services.AddCors(options => { options.AddPolicy("MyPolicy", builder => { builder.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader(); }); });
// ...
}
然后,在Controller中添加以下代码:
[EnableCors("MyPolicy")] public class MyController : ControllerBase { // ... }
其中,"MyPolicy"是自定义的策略名称,可以根据具体需要进行调整。