Request Features in ASP.NET Core
By Steve Smith
HttpContext API that applications and middleware use to process requests has an abstraction layer underneath it called feature interfaces. Each feature interface provides a granular subset of the functionality exposed by
HttpContext. These interfaces can be added, modified, wrapped, replaced, or even removed by the server or middleware as the request is processed without having to re-implement the entire
HttpContext. They can also be used to mock functionality when testing.
The Features property of
HttpContext provides access to the collection of feature interfaces 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. Some advanced features are only available by accessing the associated interface through the feature collection.
ASP.NET Core defines a number of common HTTP feature interfaces in Microsoft.AspNetCore.Http.Features, which are shared by various servers and middleware to identify the features that they support. Servers and middleware may also provide their own interfaces with additional functionality.
Most feature interfaces provide optional, light-up functionality, and their associated
HttpContext APIs provide defaults if the feature isn't present. A few interfaces are indicated in the following content as required because they provide core request and response functionality and must be implemented in order to process the request.
The following feature interfaces are from Microsoft.AspNetCore.Http.Features:
IHttpRequestFeature: Defines the structure of an HTTP request, including the protocol, path, query string, headers, and body. This feature is required in order to process requests.
IHttpResponseFeature: Defines the structure of an HTTP response, including the status code, headers, and body of the response. This feature is required in order to process requests.
IHttpResponseBodyFeature: Defines different ways of writing out the response body, using either a
PipeWriter, or a file. This feature is required in order to process requests. This replaces
IFormFeature: Used to parse and cache incoming HTTP and multipart form submissions.
IHttpBodyControlFeature: Used to control if synchronous IO operations are allowed for the request or response bodies.
IHttpActivityFeature: Used to add
Activity information for diagnostic listeners.
IHttpBufferingFeature: Defines methods for disabling buffering of requests and/or responses.
IHttpConnectionFeature: Defines properties for the connection id and local and remote addresses and ports.
IHttpMaxRequestBodySizeFeature: Controls the maximum allowed request body size for the current request.
IHttpRequestBodyDetectionFeature: Indicates if the request can have a body.
IHttpRequestIdentifierFeature: Adds a property that can be implemented to uniquely identify requests.
IHttpRequestLifetimeFeature: Defines support for aborting connections or detecting if a request has been terminated prematurely, such as by a client disconnect.
IHttpRequestTrailersFeature: Provides access to the request trailer headers, if any.
IHttpResetFeature: Used to send reset messages for protocols that support them such as HTTP/2 or HTTP/3.
IHttpResponseTrailersFeature: Enables the application to provide response trailer headers if supported.
IHttpSendFileFeature: Defines a method for sending files asynchronously.
IHttpWebSocketFeature: Defines an API for supporting web sockets.
IHttpsCompressionFeature: Controls if response compression should be used over HTTPS connections.
IQueryFeature: Parses and caches the query string.
IRequestCookiesFeature: Parses and caches the request
Cookie header values.
IResponseCookiesFeature: Controls how response cookies are applied to the
IServerVariablesFeature: This feature provides access to request server variables such as those provided by IIS.
ITlsConnectionFeature: Defines an API for retrieving client certificates.
ITlsTokenBindingFeature: Defines methods for working with TLS token binding parameters.
ITrackingConsentFeature: Used to query, grant, and withdraw user consent regarding the storage of user information related to site activity and functionality.