ASP.NET Core 中的请求功能Request Features in ASP.NET Core

作者:Steve SmithBy Steve Smith

与 HTTP 请求和响应相关的 Web 服务器实现详细信息在接口中定义。Web server implementation details related to HTTP requests and responses are defined in interfaces. 服务器实现和中间件使用这些接口来创建和修改应用程序的托管管道。These interfaces are used by server implementations and middleware to create and modify the application's hosting pipeline.

功能接口Feature interfaces

ASP.NET Core 在 Microsoft.AspNetCore.Http.Features 中定义了许多 HTTP 功能接口,服务器使用这些接口来标识其支持的功能。ASP.NET Core defines a number of HTTP feature interfaces in Microsoft.AspNetCore.Http.Features which are used by servers to identify the features they support. 以下功能接口处理请求并返回响应:The following feature interfaces handle requests and return responses:

IHttpRequestFeature 定义 HTTP 请求的结构,包括协议、路径、查询字符串、标头和正文。IHttpRequestFeature Defines the structure of an HTTP request, including the protocol, path, query string, headers, and body.

IHttpResponseFeature 定义 HTTP 响应的结构,包括状态代码、标头和响应的正文。IHttpResponseFeature Defines the structure of an HTTP response, including the status code, headers, and body of the response.

IHttpAuthenticationFeature 定义支持基于 ClaimsPrincipal 来标识用户并指定身份验证处理程序。IHttpAuthenticationFeature Defines support for identifying users based on a ClaimsPrincipal and specifying an authentication handler.

IHttpUpgradeFeature 定义对 HTTP 升级的支持,允许客户端指定在服务器需要切换协议时要使用的其他协议。IHttpUpgradeFeature Defines support for HTTP Upgrades, which allow the client to specify which additional protocols it would like to use if the server wishes to switch protocols.

IHttpBufferingFeature 定义禁用请求和/或响应缓冲的方法。IHttpBufferingFeature Defines methods for disabling buffering of requests and/or responses.

IHttpConnectionFeature 为本地和远程地址以及端口定义属性。IHttpConnectionFeature Defines properties for local and remote addresses and ports.

IHttpRequestLifetimeFeature 定义支持中止连接,或者检测是否已提前终止请求(如由于客户端断开连接)。IHttpRequestLifetimeFeature Defines support for aborting connections, or detecting if a request has been terminated prematurely, such as by a client disconnect.

IHttpSendFileFeature 定义异步发送文件的方法。IHttpSendFileFeature Defines a method for sending files asynchronously.

IHttpWebSocketFeature 定义支持 Web 套接字的 API。IHttpWebSocketFeature Defines an API for supporting web sockets.

IHttpRequestIdentifierFeature 添加一个可以实现的属性来唯一标识请求。IHttpRequestIdentifierFeature Adds a property that can be implemented to uniquely identify requests.

ISessionFeature 为支持用户会话定义 ISessionFactoryISession 抽象。ISessionFeature Defines ISessionFactory and ISession abstractions for supporting user sessions.

ITlsConnectionFeature 定义用于检索客户端证书的 API。ITlsConnectionFeature Defines an API for retrieving client certificates.

ITlsTokenBindingFeature 定义使用 TLS 令牌绑定参数的方法。ITlsTokenBindingFeature Defines methods for working with TLS token binding parameters.


ISessionFeature 不是服务器功能,而是由 SessionMiddleware 实现(请参阅管理应用程序状态)。ISessionFeature isn't a server feature, but is implemented by the SessionMiddleware (see Managing Application State).

功能集合Feature collections

HttpContextFeatures 属性为获取和设置当前请求的可用 HTTP 功能提供了一个接口。The Features property of HttpContext provides an interface for getting and setting the available HTTP features for the current request. 由于功能集合即使在请求的上下文中也是可变的,所以可使用中间件来修改集合并添加对其他功能的支持。Since the feature collection is mutable even within the context of a request, middleware can be used to modify the collection and add support for additional features.

中间件和请求功能Middleware and request features

虽然服务器负责创建功能集合,但中间件既可以添加到该集合中,也可以使用集合中的功能。While servers are responsible for creating the feature collection, middleware can both add to this collection and consume features from the collection. 例如,StaticFileMiddleware 访问 IHttpSendFileFeature 功能。For example, the StaticFileMiddleware accesses the IHttpSendFileFeature feature. 如果该功能存在,则用于从其物理路径发送所请求的静态文件。If the feature exists, it's used to send the requested static file from its physical path. 否则,使用较慢的替代方法来发送文件。Otherwise, a slower alternative method is used to send the file. 如果可用,IHttpSendFileFeature 允许操作系统打开文件并执行直接内核模式复制到网卡。When available, the IHttpSendFileFeature allows the operating system to open the file and perform a direct kernel mode copy to the network card.

另外,中间件可以添加到由服务器建立的功能集合中。Additionally, middleware can add to the feature collection established by the server. 中间件甚至可以取代现有的功能,以便增加服务器的功能。Existing features can even be replaced by middleware, allowing the middleware to augment the functionality of the server. 添加到集合中的功能稍后将在请求管道中立即用于其他中间件或基础应用程序本身。Features added to the collection are available immediately to other middleware or the underlying application itself later in the request pipeline.

通过结合自定义服务器实现和特定的中间件增强功能,可构造应用程序所需的精确功能集。By combining custom server implementations and specific middleware enhancements, the precise set of features an application requires can be constructed. 这样一来,无需更改服务器即可添加缺少的功能,并确保只公开最少的功能,从而限制攻击外围应用并提高性能。This allows missing features to be added without requiring a change in server, and ensures only the minimal amount of features are exposed, thus limiting attack surface area and improving performance.


功能接口定义给定请求可能支持的特定 HTTP 功能。Feature interfaces define specific HTTP features that a given request may support. 服务器定义功能的集合,以及该服务器支持的初始功能集,但中间件可用于增强这些功能。Servers define collections of features, and the initial set of features supported by that server, but middleware can be used to enhance these features.

其他资源Additional resources