I want to add some cors policy in my blazorwasm app template.
After generating the app using "dotnet new blazorwasm", according to https://learn.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-6.0 I add this line to Program.cs:
builder.Services.AddCors(...);
but it gave this compile error:
error CS1061: 'IServiceCollection' does not contain a definition for 'AddCors' and no accessible extension method 'AddCors' accepting a first argument of type 'IServiceCollection' could be found
It seems to be a balzor-wasm problem because if I start by "blazorserver" template it's OK.
After a little investigation I found that the 'CorsServiceCollectionExtensions' class (and many other ASP APIs) are not available. I even tried
dotnet add package Microsoft.AspNetCore.App.Ref
to add the nuget package manullay but that gave
error: Package 'Microsoft.AspNetCore.App.Ref' is incompatible with 'all' frameworks in project
As @HenkHolterman mentioned in comments, CORS is a Server feature. I was mixing Server/Client concept in Blazor. In fact I should add CORS in my web server that will host my blazor app (that could be an asp server or whatever else)
For example while creating an app using template of "blazorwasm" adding "--hosted" (dotnet new blazorwasm --hosted
) will generate an asp server app too that is the right place to call AddCors