You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<xref:Microsoft.AspNetCore.Http.HttpContext> encapsulates all information about an individual HTTP request and response. An `HttpContext` instance is initialized when an HTTP request is received. The `HttpContext` instance is accessible by middleware and app frameworks such as Web API controllers, Razor Pages, SignalR, gRPC, and more.
17
-
18
-
For more information about accessing the `HttpContext`, see <xref:fundamentals/httpcontext>.
16
+
<xref:Microsoft.AspNetCore.Http.HttpContext> encapsulates all information about an individual HTTP request and response. An `HttpContext` instance is initialized when an HTTP request is received. The `HttpContext` instance is accessible by middleware and app frameworks such as Blazor Web Apps, Web API controllers, Razor Pages, SignalR, gRPC, and more.
19
17
20
18
## `HttpRequest`
21
19
@@ -97,7 +95,7 @@ Commonly used members on `HttpResponse` include:
97
95
<xref:Microsoft.AspNetCore.Http.HttpResponse.Headers?displayProperty=nameWithType> provides access to the response headers sent with the HTTP response. There are two ways to access headers using this collection:
98
96
99
97
* Provide the header name to the indexer on the header collection. The header name isn't case-sensitive. The indexer can access any header value.
100
-
*The header collection also has properties for getting and setting commonly used HTTP headers. The properties provide a fast, IntelliSense driven way to access headers.
98
+
*Use the header collection properties for getting and setting commonly used HTTP headers. The properties provide a fast, IntelliSense driven way to access headers.
@@ -182,13 +180,13 @@ For more information about using request features and `HttpContext`, see <xref:f
182
180
183
181
## HttpContext isn't thread safe
184
182
185
-
This article primarily discusses using `HttpContext` in request and response flow from Razor Pages, controllers, middleware, etc. Consider the following when using `HttpContext` outside the request and response flow:
183
+
This article primarily discusses using `HttpContext` in request and response flow from Blazor Web App components, Razor Pages, controllers, middleware, and so forth. Consider the following when using `HttpContext` outside the request and response flow:
186
184
187
-
* The `HttpContext` is **NOT** thread safe, accessing it from multiple threads can result in exceptions, data corruption and generally unpredictable results.
188
-
* The <xref:Microsoft.AspNetCore.Http.IHttpContextAccessor> interface should be used with caution. As always, the `HttpContext` must ***not*** be captured outside of the request flow. `IHttpContextAccessor`:
189
-
* Relies on <xref:System.Threading.AsyncLocal%601> which can have a negative performance impact on asynchronous calls.
185
+
* The `HttpContext` is **NOT** thread safe. Accessing it from multiple threads can result in unpredictable results, such as exceptions and data corruption.
186
+
* The <xref:Microsoft.AspNetCore.Http.IHttpContextAccessor> interface should be used with caution. As always, the `HttpContext` must ***not*** be captured outside of the request flow. `IHttpContextAccessor`:
187
+
* Relies on <xref:System.Threading.AsyncLocal%601>, which can have a negative performance impact on asynchronous calls.
190
188
* Creates a dependency on "ambient state" which can make testing more difficult.
191
-
*<xref:Microsoft.AspNetCore.Http.IHttpContextAccessor.HttpContext%2A?displayProperty=nameWithType>may be `null` if accessed outside of the request flow.
189
+
*<xref:Microsoft.AspNetCore.Http.IHttpContextAccessor.HttpContext%2A?displayProperty=nameWithType>might be `null` if accessed outside of the request flow.
192
190
* To access information from `HttpContext` outside the request flow, copy the information inside the request flow. Be careful to copy the actual data and not just references. For example, rather than copying a reference to an `IHeaderDictionary`, copy the relevant header values or copy the entire dictionary key by key before leaving the request flow.
193
191
* Don't capture `IHttpContextAccessor.HttpContext` in a constructor.
194
192
@@ -219,3 +217,7 @@ The application also includes `PeriodicBranchesLoggerService`, which logs the op
219
217
`PeriodicBranchesLoggerService` is a [hosted service](xref:fundamentals/host/hosted-services), which runs outside the request and response flow. Logging from the `PeriodicBranchesLoggerService` has a null `HttpContext`. The `PeriodicBranchesLoggerService` was written to not depend on the `HttpContext`.
0 commit comments