gpt4 book ai didi

c# - NET Core Cors 抛出错误

转载 作者:太空宇宙 更新时间:2023-11-03 22:41:35 24 4
gpt4 key购买 nike

所以我有以下内容:

public void ConfigureServices(IServiceCollection services)
{
var corsBuilder = new CorsPolicyBuilder();
corsBuilder.AllowAnyHeader();
corsBuilder.AllowAnyMethod();
corsBuilder.AllowAnyOrigin();
corsBuilder.AllowCredentials();

services.AddCors(options =>
{
options.AddPolicy("AllowAllOrigins", corsBuilder.Build());
});

services.AddDbContextPool<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();

services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddJsonOptions(
options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
);

services.Configure<Infrastructure.FileOptions>(Configuration.GetSection("Files"));
services.AddTransient<Infrastructure.ServiceCollection>();
}

如您所见,我允许所有人访问我的路线。

但是,当我尝试访问路由时出现以下错误:

Failed to load https://localhost:44381/test: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.

我做错了什么?

最佳答案

你应该在配置方法中添加这一行

app.UseCors("AllowAllOrigins");

这会激活您在服务中配置的 AllowAllOrigins

关于c# - NET Core Cors 抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51963723/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com