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

作者:Steve SmithBy Steve Smith

有關 HTTP 要求和回應的網頁伺服器實作詳細資料,定義於介面中。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