将 headers 添加到 .NET 6 中 Blazor WASM 的每个响应
Adding headers to every response of an Blazor WASM in .NET 6
我在 .NET 6 中创建了一个简单的(到目前为止)Blazor WebAssembly 应用程序。
我目前正在向应用程序的每个响应添加额外的 HTTP 请求,并想添加一个 X-FRAME-OPTIONS header,但是在搜索如何做时,我意识到我没有知道如何处理它。
首先,这是我的 Program.cs
文件:
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using MyApplicationNamespace;
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
await builder.Build().RunAsync();
看的时候this webpage学会了里面使用中间件
app.Use(async (context, next) =>
{
context.Response.Headers.Add("x-my-custom-header", "middleware response");
await next();
});
我明白 from this site 为了使用 Use 函数,我可以这样做:
var app = builder.Build();
app.Use();
或者我可以只传递一个委托函数
app.Run(async context =>
{
await context.Response.WriteAsync("Hello from 2nd delegate.");
});
要点是,在 Blazor WASM
中我没有 运行 方法,并且 运行Async 不接受参数。
我不确定从这里到哪里才能添加 header?
我是否遗漏了一段 NuGet 内容?
根据我从 this person on Twitter 中学到的知识:
The Blazor WASM application is the client. It lives exclusively within the browser. It is receiving responses from a web server, and not "returning" anything. X-Frame-Options headers need to be set on the server, not by the application in the Browser.
Do you mean that the web server should add these headers when it's delivering the (static) files of your Blazor application to the browser before it starts being executed there? You need to configure your web server (whatever it is) to send these headers then.
由于我将我的应用程序部署为 Azure 应用服务,我使用高级工具编辑了 web.config
,灵感来自 this site。
我在 .NET 6 中创建了一个简单的(到目前为止)Blazor WebAssembly 应用程序。
我目前正在向应用程序的每个响应添加额外的 HTTP 请求,并想添加一个 X-FRAME-OPTIONS header,但是在搜索如何做时,我意识到我没有知道如何处理它。
首先,这是我的 Program.cs
文件:
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using MyApplicationNamespace;
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
await builder.Build().RunAsync();
看的时候this webpage学会了里面使用中间件
app.Use(async (context, next) =>
{
context.Response.Headers.Add("x-my-custom-header", "middleware response");
await next();
});
我明白 from this site 为了使用 Use 函数,我可以这样做:
var app = builder.Build();
app.Use();
或者我可以只传递一个委托函数
app.Run(async context =>
{
await context.Response.WriteAsync("Hello from 2nd delegate.");
});
要点是,在 Blazor WASM
中我没有 运行 方法,并且 运行Async 不接受参数。
我不确定从这里到哪里才能添加 header?
我是否遗漏了一段 NuGet 内容?
根据我从 this person on Twitter 中学到的知识:
The Blazor WASM application is the client. It lives exclusively within the browser. It is receiving responses from a web server, and not "returning" anything. X-Frame-Options headers need to be set on the server, not by the application in the Browser.
Do you mean that the web server should add these headers when it's delivering the (static) files of your Blazor application to the browser before it starts being executed there? You need to configure your web server (whatever it is) to send these headers then.
由于我将我的应用程序部署为 Azure 应用服务,我使用高级工具编辑了 web.config
,灵感来自 this site。