[英]Disabling CORS in Minimal API Ocelot Gateway
所以問題是我的前端有 Blazor WebAssembly,通過 Ocelot API 網關進行 API 調用,但由於某種原因 CORS 失敗
但是在Program.cs
的Ocelot Gateway
中我有
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.SetBasePath(builder.Environment.ContentRootPath)
.AddJsonFile("ocelot.json", optional: false, reloadOnChange:true)
.AddEnvironmentVariables();
builder.Services.AddOcelot(builder.Configuration);
var app = builder.Build();
await app.UseOcelot();
app.UseCors(builder => builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
app.Run();
像這樣也行不通:
builder.Services.AddCors(options =>
{
options.AddPolicy("asd",
policy =>
{
policy.AllowAnyOrigin().AllowAnyMethod();
});
});
var app = builder.Build();
await app.UseOcelot();
app.UseCors("asd");
app.Run();
我應該怎么做才能訪問任何來源或擺脫這個 cors? 我幾乎嘗試了所有方法,但似乎沒有什么能解決我的問題。
解決方案:
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.SetBasePath(builder.Environment.ContentRootPath)
.AddJsonFile("ocelot.json", optional: false, reloadOnChange:true)
.AddEnvironmentVariables();
builder.Services.AddOcelot(builder.Configuration);
builder.Services.AddCors(); // Add cors
var app = builder.Build();
app.UseCors(builder => builder // Allow any
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
await app.UseOcelot();
app.Run();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.