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

作者:Steve SmithBy Steve Smith

HttpContext應用程式和中介軟體用來處理要求的 API,在其下稱為 功能介面 的抽象層。The HttpContext API that applications and middleware use to process requests has an abstraction layer underneath it called feature interfaces. 每個功能介面都會提供所公開之功能的詳細子集 HttpContextEach feature interface provides a granular subset of the functionality exposed by HttpContext. 這些介面可以加入、修改、包裝、取代,或甚至由伺服器或中介軟體移除,因為要求會經過處理,而不需要重新執行整個 HttpContextThese 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.

功能集合Feature collections

Features 屬性 HttpContext 提供目前要求之功能介面集合的存取權。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. 某些 advanced 功能只能透過功能集合存取相關聯的介面來使用。Some advanced features are only available by accessing the associated interface through the feature collection.

功能介面Feature interfaces

ASP.NET Core 定義中的一些常見 HTTP 功能介面 Microsoft.AspNetCore.Http.Features ,這些介面會由各種伺服器和中介軟體共用,以識別它們所支援的功能。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.

大部分的功能介面都會提供選擇性的亮顯功能,而其相關聯 HttpContext 的 api 會在功能不存在時提供預設值。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.

下列功能介面來自 Microsoft.AspNetCore.Http.FeaturesThe following feature interfaces are from Microsoft.AspNetCore.Http.Features:

IHttpRequestFeature:定義 HTTP 要求的結構,包括通訊協定、路徑、查詢字串、標頭和主體。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:定義 HTTP 回應的結構,包括狀態碼、標頭和回應主體。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:定義使用、或檔案寫出回應主體的不同方式 Stream PipeWriterIHttpResponseBodyFeature: Defines different ways of writing out the response body, using either a Stream, a PipeWriter, or a file. 需要此功能才能處理要求。This feature is required in order to process requests. 這會取代 IHttpResponseFeature.BodyIHttpSendFileFeatureThis replaces IHttpResponseFeature.Body and IHttpSendFileFeature.

IHttpAuthenticationFeature:保留 ClaimsPrincipal 目前與要求相關聯的。IHttpAuthenticationFeature: Holds the ClaimsPrincipal currently associated with the request.

IFormFeature:用來剖析和快取傳入的 HTTP 和多部分表單提交。IFormFeature: Used to parse and cache incoming HTTP and multipart form submissions.

IHttpBodyControlFeature:用來控制要求或回應主體是否允許同步 IO 作業。IHttpBodyControlFeature: Used to control if synchronous IO operations are allowed for the request or response bodies.

IHttpBufferingFeature:定義停用要求和/或回應之緩衝的方法。IHttpBufferingFeature: Defines methods for disabling buffering of requests and/or responses.

IHttpConnectionFeature:定義連接識別碼和本機和遠端位址和埠的屬性。IHttpConnectionFeature: Defines properties for the connection id and local and remote addresses and ports.

IHttpMaxRequestBodySizeFeature:控制目前要求所允許的要求主體大小上限。IHttpMaxRequestBodySizeFeature: Controls the maximum allowed request body size for the current request.

IHttpRequestBodyDetectionFeature:指出要求是否可以有主體。IHttpRequestBodyDetectionFeature: Indicates if the request can have a body.

IHttpRequestIdentifierFeature:加入可實作為唯一識別要求的屬性。IHttpRequestIdentifierFeature: Adds a property that can be implemented to uniquely identify requests.

IHttpRequestLifetimeFeature:定義中止連接或偵測要求是否已提前終止的支援,例如用戶端中斷連線。IHttpRequestLifetimeFeature: Defines support for aborting connections or detecting if a request has been terminated prematurely, such as by a client disconnect.

IHttpRequestTrailersFeature:提供要求尾端標頭(如果有)的存取權。IHttpRequestTrailersFeature: Provides access to the request trailer headers, if any.

IHttpResetFeature:用來傳送重設訊息給支援它們的通訊協定,例如 HTTP/2 或 HTTP/3。IHttpResetFeature: Used to send reset messages for protocols that support them such as HTTP/2 or HTTP/3.

IHttpResponseTrailersFeature:可讓應用程式在支援時提供回應尾端標頭。IHttpResponseTrailersFeature: Enables the application to provide response trailer headers if supported.

IHttpSendFileFeature:定義以非同步方式傳送檔案的方法。IHttpSendFileFeature: Defines a method for sending files asynchronously.

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.

IHttpWebSocketFeature:定義支援 web 通訊端的 API。IHttpWebSocketFeature: Defines an API for supporting web sockets.

IHttpsCompressionFeature:控制是否應透過 HTTPS 連接使用回應壓縮。IHttpsCompressionFeature: Controls if response compression should be used over HTTPS connections.

IItemsFeature:儲存 Items 每個要求應用程式狀態的集合。IItemsFeature: Stores the Items collection for per request application state.

IQueryFeature:剖析和快取查詢字串。IQueryFeature: Parses and caches the query string.

IRequestBodyPipeFeature:以形式表示要求主體 PipeReaderIRequestBodyPipeFeature: Represents the request body as a PipeReader.

IRequestCookiesFeature:剖析和快取要求 Cookie 標頭值。IRequestCookiesFeature: Parses and caches the request Cookie header values.

IResponseCookiesFeature:控制如何將回應套用 cookie 至 Set-Cookie 標頭。IResponseCookiesFeature: Controls how response cookies are applied to the Set-Cookie header.

IServerVariablesFeature:這項功能可讓您存取要求伺服器變數,例如 IIS 所提供的變數。IServerVariablesFeature: This feature provides access to request server variables such as those provided by IIS.

IServiceProvidersFeature:提供 IServiceProvider 具有範圍要求服務的存取權。IServiceProvidersFeature: Provides access to an IServiceProvider with scoped request services.

ISessionFeature:定義 ISessionFactory ISession 支援使用者會話的和抽象概念。ISessionFeature: Defines ISessionFactory and ISession abstractions for supporting user sessions. ISessionFeature 是由 (所執行, SessionMiddleware 請參閱 ASP.NET Core 中的會話) 。ISessionFeature is implemented by the SessionMiddleware (see ASP.NET Core 中的會話).

ITlsConnectionFeature:定義用於抓取用戶端憑證的 API。ITlsConnectionFeature: Defines an API for retrieving client certificates.

ITlsTokenBindingFeature:定義使用 TLS 權杖系結參數的方法。ITlsTokenBindingFeature: Defines methods for working with TLS token binding parameters.

ITrackingConsentFeature:用來查詢、授與及撤銷使用者關於與網站活動和功能相關之使用者資訊儲存的同意。ITrackingConsentFeature: Used to query, grant, and withdraw user consent regarding the storage of user information related to site activity and functionality.

其他資源Additional resources