且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Asp.net核心默认标头

更新时间:2023-02-16 17:53:38

这与ASP.NET无关;那些标题正在发送给客户。

That's nothing to do with ASP.NET; those headers are being sent the client.

接收到具有不同字段名称的标头字段的顺序并不重要。但是,首先发送通用标头字段是好习惯,其次是request-header或response-header字段,以entity-header字段结尾。

The order in which header fields with differing field names are received is not significant. However, it is "good practice" to send general-header fields first, followed by request-header or response-header fields, and ending with the entity-header fields.



大多数客户端都会放置常规标题 - 比如 Connection Keep-Alive - 在任何其他标题之前。



依赖于发送的标题按特定顺序非常脆弱,几乎会立即中断。



相反,你应该测试header字典是否包含预期的标题:


Most clients will put "general" headers - like Connection and Keep-Alive - before any other headers.

Relying on headers being sent in a specific order is extremely fragile, and will break almost immediately.

Instead, you should test whether the headers dictionary contains the expected header:

public async Task Invoke(HttpContext httpContext)
{
    if (!httpContext.Request.Headers.ContainsKey("UserKey"))
    {
        throw new InvalidOperationException("Required header is missing.");
    }

    await _next(httpContext);
}