So I’m for some days now trying to solve a cors error problem i’m having, however I already did a lot of tries on “Program.cs” file to try to solve this but I can’t figure this out..
Basically the cors error only happen on post methods, I’m using this on Program.cs
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddHttpClient();
builder.Services.AddCors(builder =>
{
builder.AddPolicy("AllowAll", options =>
{
options.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseCors("AllowAll");
app.UseAuthorization();
app.MapControllers();
app.Run();
On the browser console I see this:
Access to XMLHttpRequest at ‘https://localhost:7278/api/Topaz/SetTabletState’ from origin ‘https://example.com’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
Can anyone help me figuring this out?
I already tried to put the domain on the origins but still does not work.
I already tried to define the cors policy in the controller but still does not work.
Fairydx is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.