我想添加一些cors策略在我的blazorwasm应用程序模板。
根据https://learn.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-6.0的说法,在使用"dotnet“生成应用程序之后,我将这一行添加到Program.cs中:
builder.Services.AddCors(...);
但是它给出了这个编译错误:
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
这似乎是一个平衡-是个问题,因为如果我开始使用"blazorserver“模板,就可以了。
经过一些调查后,我发现'CorsServiceCollectionExtensions‘类(和许多其他ASP )不可用。我甚至试过
dotnet add package Microsoft.AspNetCore.App.Ref
添加nuget包manullay,但是
error: Package 'Microsoft.AspNetCore.App.Ref' is incompatible with 'all' frameworks in project
发布于 2022-07-19 07:31:40
正如评论中提到的@HenkHolterman,CORS是一个Server特性。我在Blazor中混合了服务器/客户端的概念。实际上,我应该在我的web服务器中添加CORS,它将承载我的blazor应用程序(可能是asp服务器或其他任何东西)。
例如,在创建一个使用"blazorwasm“模板的应用程序时,添加”-hosted“(dotnet new blazorwasm --hosted
)也会生成一个asp服务器应用程序,该应用程序也是调用AddCors
的合适位置。
https://stackoverflow.com/questions/72759424
复制相似问题