app.Use((context, next) => {
context.Response.Headers.Add("Access-Control-Allow-Origin", "");
return next.Invoke();
});
将
打开IIS管理器,选择你的应用程序。 在'中间件”下,在'跨域资源共享(CORS)”下点击'启用”。 接着,使用以下代码'Access-Control-Allow-Origin”标头添加到响应中:
response.Headers.Add("Access-Control-Allow-Origin", "
将
在Blazor应用文件夹中安装融合注入Blazor代理库。
打开"Program.cs"文件,并向主机构建器添加以下内容:
builder.Services.AddBlazorHybrid(); builder.Services.AddSingleton(sp => new HttpClient { BaseAddress = new Uri("https://example.com"), DefaultRequestHeaders = { // Send the authentication token with every request Authorization = new AuthenticationHeaderValue("Bearer", token) } });
在你的Blazor Razor页面中,添加代理配置:
@page "/" @using Microsoft.AspNetCore.Components.WebAssembly.Services @inject IProxyDefinitionCollection ProxyDefinitions
@code { protected override async Task OnInitializedAsync() { // register the proxy definition
await ProxyDefinitions.AddAsync(new ProxyDefinition
{
// The backend base address