向 .NET 6 中 Blazor WASM 的每个响应添加标头

发布于 2025-01-19 13:21:36 字数 1568 浏览 0 评论 0原文

我在 .NET 6 中创建了一个简单的(到目前为止)Blazor WebAssembly 应用程序。

我目前正在向应用程序的每个响应添加额外的 HTTP 请求,并希望添加 X-FRAME-OPTIONS 标头,但在搜索如何执行此操作时,我意识到我不知道如何处理它。

对于初学者来说,这是我的 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 时网页 我了解了如何在里面使用中间件

app.Use(async (context, next) =>
{
    context.Response.Headers.Add("x-my-custom-header", "middleware response");
    await next();
});

我确实理解从这个网站得知,为了使用 Use 函数,我可以这样做:

var app = builder.Build();
app.Use();

或者我可以只传递一个委托函数

app.Run(async context =>
{
    await context.Response.WriteAsync("Hello from 2nd delegate.");
});

要点是,在 Blazor WASM 中我没有 Run 方法,并且 RunAsync 不带参数。

我不确定从这里到哪里添加标题?

我错过了 NuGet 段落吗?

I created a simple (so far) Blazor WebAssembly application in .NET 6.

I'm currently adding additional HTTP requests to every response of the application and wanted to add an X-FRAME-OPTIONS header, but when searching on how to do it, I realized I don't know how to approach it.

For starters here's my Program.cs file:

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();

When reading this webpage I learned about using middleware inside

app.Use(async (context, next) =>
{
    context.Response.Headers.Add("x-my-custom-header", "middleware response");
    await next();
});

I do understand from this site that in order to use the Use function I can do this:

var app = builder.Build();
app.Use();

Or that I can just pass a delegate function

app.Run(async context =>
{
    await context.Response.WriteAsync("Hello from 2nd delegate.");
});

Point is, in Blazor WASM I don't have a Run method, and RunAsync does not take parameters.

I'm not sure where to go from here to add a header?

Am I missing a NuGet passage?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请你别敷衍 2025-01-26 13:21:36

从我从

Glazor WASM应用程序是客户。它仅生活在浏览器中。它正在从Web服务器接收响应,而不是“返回”任何内容。需要在服务器上设置X框架标题,而不是通过浏览器中的应用程序设置。

您的意思是,当Web服务器在将其在浏览器开始执行之前将其(静态)文件传递到浏览器时,应该添加这些标头? 您需要配置您的Web服务器(无论是什么)即可发送这些标头。

因为我将应用程序部署为 Azure App Service 我使用高级工具来编辑<< code> web.config 受

From what I've learned from 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.

Since I deployed my application as an Azure App Service I used Advanced Tools to edit out web.config inspired by this site.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文