我有一个 Razor 页面,想要从请求中读取标头,但我找不到如何执行此操作。我该怎么做?
@page "/"
@inject NavigationManager navigationManager;
<div class="section">
</div>
@code {
private string header;
protected override async Task OnInitializedAsync()
{
// how to do it here
}
}
Run Code Online (Sandbox Code Playgroud)
Rus*_*sso 12
我找到了阅读标题的简单方法
首先,添加 IHttpContextAccessor 服务的默认实现
builder.Services.AddHttpContextAccessor();
Run Code Online (Sandbox Code Playgroud)
然后需要从页面请求中通过标头名称获取标头:
@inject IHttpContextAccessor httpContextAccessor;
@code {
private string needParameter { get; set; }
protected override async Task OnInitializedAsync()
{
needParameter = httpContextAccessor.HttpContext.Request.Headers["parameterName"];
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2753 次 |
| 最近记录: |