gpt4 book ai didi

c# - ASP.Net Core 2.0 如何获取中间件中的所有请求头?

转载 作者:行者123 更新时间:2023-12-01 22:18:02 25 4
gpt4 key购买 nike

在 ASP.Net Core 2.0 中,我尝试验证自定义中间件中传入的请求 header 。

问题是我不知道如何提取所有键值对 header 。我需要的 header 存储在 protected 属性中

protected Dictionary<string, stringValues> MaybeUnknown

到目前为止,我的中间件类看起来像这样:

public class HeaderValidation
{
private readonly RequestDelegate _next;
public HeaderValidation(RequestDelegate next)
{
_next = next;
}

public async Task Invoke(HttpContext httpContext)
{
IHeaderDictionary headers = httpContext.Request.Headers; // at runtime headers are of type FrameRequestHeaders

// How to get the key-value-pair headers?
// "protected Dictionary<string, stringValues> MaybeUnknown" from headers is inaccessbile due to its protection level
// Casting headers as Dictionary<string, StringValues> results in null

await _next.Invoke(httpContext);
}
}

我的目标是提取所有请求 header ,而不仅仅是提取一些我必须知道其特定键的选定 header 。

最佳答案

httpContext.Request.Headers 是一个字典。您可以通过将 header 名称作为键传递来返回 header 的值:

context.Request.Headers["Connection"].ToString()

关于c# - ASP.Net Core 2.0 如何获取中间件中的所有请求头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49307762/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com