CORS blocks direct access to API from Blazor - Stack Overflow

I've setup an Aspire solution with a Blazor WebAssembly and an API projects. API runs on https:l

I've setup an Aspire solution with a Blazor WebAssembly and an API projects. API runs on https://localhost:7476 and Blazor is served from https://localhost:7077. Generally any API registered with MapPost or MapGet is working as expected because by default Blazor app runs in InteractiveServer mode, so all APIs appear to be called via internal SignalR connection.

However for uploading files from the web, I need to hit the API endpoint directly. I'm using RadzenUpload component to do so, and I've configured it to hit https://localhost:7476/api/upload

However the browser gives me an error:

Access to XMLHttpRequest at 'https://localhost:7476/api/upload' from origin 'https://localhost:7077' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I've tried adding the following to both the API and the Blazor's Program.cs but that didn't help. Any help is greatly appreciated.

builder.Services.AddCors(o => o.AddPolicy("AllowAll", builder =>
{
    builder.AllowAnyOrigin()
        .AllowAnyMethod()
        .AllowAnyHeader();
}));

...

var app = builder.Build();

app.UseCors("AllowAll");

I've setup an Aspire solution with a Blazor WebAssembly and an API projects. API runs on https://localhost:7476 and Blazor is served from https://localhost:7077. Generally any API registered with MapPost or MapGet is working as expected because by default Blazor app runs in InteractiveServer mode, so all APIs appear to be called via internal SignalR connection.

However for uploading files from the web, I need to hit the API endpoint directly. I'm using RadzenUpload component to do so, and I've configured it to hit https://localhost:7476/api/upload

However the browser gives me an error:

Access to XMLHttpRequest at 'https://localhost:7476/api/upload' from origin 'https://localhost:7077' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I've tried adding the following to both the API and the Blazor's Program.cs but that didn't help. Any help is greatly appreciated.

builder.Services.AddCors(o => o.AddPolicy("AllowAll", builder =>
{
    builder.AllowAnyOrigin()
        .AllowAnyMethod()
        .AllowAnyHeader();
}));

...

var app = builder.Build();

app.UseCors("AllowAll");

Share Improve this question asked Nov 20, 2024 at 4:19 beekeeperbeekeeper 6221 gold badge6 silver badges21 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

The error indicates the response from server lacks header Access-Control-Allow-Origin (which would be appended by app.UseCors() middleware), So you don't have to add the cors related codes in your blazor app,just add the codes in your webapi project

Also,try modify your policy to

builder.Services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
{
    builder.WithOrigins("https://localhost:{port of your blazor app}")
        .AllowAnyMethod()
        .AllowAnyHeader()
        .AllowCredentials();
}));
......

app.UseCors("MyPolicy");

if you are working on controller based webapi,notice the order of the middleware,follow the sample in official document

It now works on myside:

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742382458a4433392.html

相关推荐

  • CORS blocks direct access to API from Blazor - Stack Overflow

    I've setup an Aspire solution with a Blazor WebAssembly and an API projects. API runs on https:l

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信