ARTAV
ARTAV

Reputation: 102

Different mechanism of HttpClient in Blazor server side and Web assembly

I want to get data from ASP.net Core API with HttpClient Factory. I use Microsoft.Extensions.Http package like this :

// Register service in IOC containter 
builder.Services.AddHttpClient<IProductService, ProductService>(option =>
                {
                    option.BaseAddress = new Uri(""/*Base url*/);
                });

// Use in service 
var stream = await _httpClient.GetStreamAsync("");

When I use code in Blazor server side and works correctly. But when I use code in Blazor wasm throw Exception

Access to fetch at 'http client factory base url' from origin 'blazor wasm app url' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

No change in ASP.net core API and different results. I use .net core 3.1 in all apps Thanks

Upvotes: 2

Views: 1200

Answers (1)

ARTAV
ARTAV

Reputation: 102

I activate CORS in my API Like this

// In ConfigureServices method
    options.AddPolicy("OpenCors", builder =>
                {
                    builder
                          .AllowAnyOrigin()
                        .AllowAnyHeader()
                        .AllowAnyMethod()
                      ;
});

// In Configure method
app.UseCors("OpenCors");

                

Upvotes: 0

Related Questions