向 .NET 6 中 Blazor WASM 的每个响应添加标头
我在 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从我从
因为我将应用程序部署为 Azure App Service 我使用高级工具来编辑<< code> web.config 受。
From what I've learned from this person on Twitter:
Since I deployed my application as an Azure App Service I used Advanced Tools to edit out
web.config
inspired by this site.