【发布时间】:2022-08-02 17:37:53
【问题描述】:
初始情况
我目前正在构建一个 Blazor WebAssembly 应用程序,它显示来自我的 ASP.NET Core 6 API 的数据。请注意,这些项目分为两种不同的解决方案。
问题是我的 API 拒绝了由我的 WASM 应用程序发送的请求。显然这与我的 API 的 CORS 配置有关。
Access to fetch at \'https://localhost:7030/api/v1/test\' from origin \'https://localhost:7198\' 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. If an opaque response serves your needs, set the request\'s mode to \'no-cors\' to fetch the resource with CORS disabled.
API 的 CORS 配置基于 this answer by Aae Que。
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddDefaultPolicy(builder =>
builder.WithOrigins(\"https://localhost:44338\")
.AllowAnyMethod()
.AllowAnyHeader()
);
});
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseCors();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
有人知道我如何解决我的问题吗?
我非常感谢任何形式的帮助,干杯! :)
编辑 1
出于兴趣,我将项目重写为 Blazor Server。显然,无论是否使用 API 的 CORS 配置,Blazor 服务器应用程序发送的请求都可以正常工作。
-
我们看不到
\"Cors:Origins\"。您将需要:7198某处。
标签: asp.net iis blazor httpclient blazor-webassembly