我想通过HttpClient Factory从ASP.net Core API获取数据。 我使用像这样的Microsoft.Extensions.Http包:
// 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("");
当我在Blazor服务器端使用代码并正常工作时。但是当我在Blazor wasm中使用代码时会抛出异常
通过CORS策略阻止从来源“ blasor wasm应用程序URL”获取“ http客户端工厂基本URL”处的访问:请求的资源上没有“ Access-Control-Allow-Origin”标头。如果不透明的响应可以满足您的需求,请将请求的模式设置为“ no-cors”,以在禁用CORS的情况下获取资源。
ASP.net核心API不变,结果不同。 我在所有应用程序中都使用.net Core 3.1 谢谢
答案 0 :(得分:0)
我这样激活我的API中的CORS
// In ConfigureServices method
options.AddPolicy("OpenCors", builder =>
{
builder
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod()
;
});
// In Configure method
app.UseCors("OpenCors");