我正在为我的项目使用 Owin 中间件,我想为每个 http 请求添加一个自定义标头。我正在使用如下所示的代码:
public class RequestIdMiddleware : OwinMiddleware
{
public RequestIdMiddleware(OwinMiddleware next)
: base(next)
{
}
public override Task Invoke(IOwinContext context)
{
var request = context.Request;
var headers = request.Headers;
var headerKey = "X-Client-RequestID";
// custom header addition
if (!headers.ContainsKey(headerKey))
{
context.Request.Headers[headerKey] = Guid.NewGuid().ToString();
}
// continue pipeline
return Next.Invoke(context);
}
}
我已正确注册并使用了该类,但在控制器的 Request.Headers.GetValue("X-Client-RequestID") 中没有看到标头。