aspnetcore/migration/fx-to-core/areas/http-context.md
HttpContext is a fundamental component of web applications, providing access to HTTP request and response information. When migrating from ASP.NET Framework to ASP.NET Core, HttpContext presents unique challenges because the two frameworks have different APIs and approaches.
ASP.NET Framework and ASP.NET Core have fundamentally different HttpContext implementations:
These differences mean you can't simply move your HttpContext code from Framework to Core without changes.
You have two main approaches for handling HttpContext during migration:
For most applications, migrating to ASP.NET Core's native HttpContext provides the best performance and maintainability. However, larger applications or those with extensive HttpContext usage may benefit from using System.Web adapters during incremental migration.
You have two main options for migrating HttpContext from ASP.NET Framework to ASP.NET Core. Your choice depends on your migration timeline, whether you need to run both applications simultaneously, and how much code you're willing to rewrite.
Answer these questions to choose your approach:
Are you doing a complete rewrite or incremental migration?
Do you have extensive HttpContext usage across shared libraries?
| Approach | Code Changes | Performance | Shared Libraries | When to Use |
|---|---|---|---|---|
| Complete rewrite | High - Rewrite all HttpContext code | Best | Requires updates | Complete rewrites, performance-critical apps |
| System.Web adapters | Low - Keep existing patterns | Good | Works with existing code | Incremental migrations, extensive HttpContext usage |
The adapters are backed by xref:Microsoft.AspNetCore.Http.HttpContext which cannot be used past the lifetime of a request. Thus, xref:System.Web.HttpContext when run on ASP.NET Core cannot be used past a request as well, while on ASP.NET Framework it would work at times. An xref:System.ObjectDisposedException will be thrown in cases where it is used past a request end.
Recommendation: Store the values needed into a POCO and hold onto that.
[!WARNING] ASP.NET Core does not guarantee thread affinity for requests. If your code requires thread-safe access to
HttpContext, you must ensure proper synchronization.
In ASP.NET Framework, a request had thread-affinity and xref:System.Web.HttpContext.Current would only be available if on that thread. ASP.NET Core does not have this guarantee so xref:System.Web.HttpContext.Current will be available within the same async context, but no guarantees about threads are made.
Recommendation: If reading/writing to the xref:System.Web.HttpContext, you must ensure you are doing so in a single-threaded way. You can force a request to never run concurrently on any async context by setting the ISingleThreadedRequestMetadata. This will have performance implications and should only be used if you can't refactor usage to ensure non-concurrent access. There is an implementation available to add to controllers with SingleThreadedRequestAttribute:
[SingleThreadedRequest]
public class SomeController : Controller
{
...
}
By default, the incoming request is not always seekable nor fully available. In order to get behavior seen in .NET Framework, you can opt into prebuffering the input stream. This will fully read the incoming stream and buffer it to memory or disk (depending on settings).
Recommendation: This can be enabled by applying endpoint metadata that implements the IPreBufferRequestStreamMetadata interface. This is available as an attribute PreBufferRequestStreamAttribute that can be applied to controllers or methods.
To enable this on all MVC endpoints, there is an extension method that can be used as follows:
app.MapDefaultControllerRoute()
.PreBufferRequestStream();
Some APIs on xref:System.Web.HttpContext.Response require that the output stream is buffered, such as xref:System.Web.HttpResponse.Output, xref:System.Web.HttpResponse.End, xref:System.Web.HttpResponse.Clear, and xref:System.Web.HttpResponse.SuppressContent.
Recommendation: In order to support behavior for xref:System.Web.HttpContext.Response that requires buffering the response before sending, endpoints must opt-into it with endpoint metadata implementing IBufferResponseStreamMetadata.
To enable this on all MVC endpoints, there is an extension method that can be used as follows:
app.MapDefaultControllerRoute()
.BufferResponseStream();
Choose this approach when you're performing a complete migration and can rewrite HttpContext-related code to use ASP.NET Core's native implementation.
ASP.NET Core's HttpContext provides a more modular and extensible design compared to ASP.NET Framework. This approach offers the best performance but requires more code changes during migration.
HttpContext has significantly changed in ASP.NET Core. When migrating HTTP modules or handlers to middleware, you'll need to update your code to work with the new HttpContext API.
In ASP.NET Core middleware, the Invoke method takes a parameter of type HttpContext:
public async Task Invoke(HttpContext context)
This HttpContext is different from the ASP.NET Framework version and requires different approaches to access request and response information.
This section shows how to translate the most commonly used properties of xref:System.Web.HttpContext?displayProperty=fullName to the equivalent xref:Microsoft.AspNetCore.Http.HttpContext?displayProperty=fullName in ASP.NET Core.
xref:System.Web.HttpContext.Items?displayProperty=nameWithType → xref:Microsoft.AspNetCore.Http.HttpContext.Items?displayProperty=nameWithType
No equivalent → xref:Microsoft.AspNetCore.Http.HttpContext.TraceIdentifier?displayProperty=nameWithType
Unique request ID for logging
xref:System.Web.HttpRequest.HttpMethod?displayProperty=nameWithType → xref:Microsoft.AspNetCore.Http.HttpRequest.Method?displayProperty=nameWithType
xref:System.Web.HttpRequest.QueryString?displayProperty=nameWithType → xref:Microsoft.AspNetCore.Http.HttpRequest.QueryString?displayProperty=nameWithType
xref:System.Web.HttpRequest.Url?displayProperty=nameWithType / xref:System.Web.HttpRequest.RawUrl?displayProperty=nameWithType → Multiple properties
Use Request.Scheme, Host, PathBase, Path, QueryString
xref:System.Web.HttpRequest.IsSecureConnection?displayProperty=nameWithType → xref:Microsoft.AspNetCore.Http.HttpRequest.IsHttps?displayProperty=nameWithType
xref:System.Web.HttpRequest.UserHostAddress?displayProperty=nameWithType → xref:Microsoft.AspNetCore.Http.ConnectionInfo.RemoteIpAddress?displayProperty=nameWithType
xref:System.Web.HttpRequest.Cookies?displayProperty=nameWithType → xref:Microsoft.AspNetCore.Http.HttpRequest.Cookies?displayProperty=nameWithType
xref:System.Web.HttpRequest.RequestContext?displayProperty=nameWithType → xref:Microsoft.AspNetCore.Routing.RoutingHttpContextExtensions.GetRouteData*?displayProperty=nameWithType
xref:System.Web.HttpRequest.Headers?displayProperty=nameWithType → xref:Microsoft.AspNetCore.Http.HttpRequest.Headers?displayProperty=nameWithType
xref:System.Web.HttpRequest.UserAgent?displayProperty=nameWithType → xref:Microsoft.AspNetCore.Http.HttpRequest.Headers?displayProperty=nameWithType
xref:System.Web.HttpRequest.UrlReferrer?displayProperty=nameWithType → xref:Microsoft.AspNetCore.Http.HttpRequest.Headers?displayProperty=nameWithType
xref:System.Web.HttpRequest.ContentType?displayProperty=nameWithType → xref:Microsoft.AspNetCore.Http.HttpRequest.ContentType?displayProperty=nameWithType
xref:System.Web.HttpRequest.Form?displayProperty=nameWithType → xref:Microsoft.AspNetCore.Http.HttpRequest.Form?displayProperty=nameWithType
Warning: Read form values only if content type is x-www-form-urlencoded or form-data
xref:System.Web.HttpRequest.InputStream?displayProperty=nameWithType → xref:Microsoft.AspNetCore.Http.HttpRequest.Body?displayProperty=nameWithType
Warning: Use only in handler middleware at end of pipeline. Body can only be read once per request
xref:System.Web.HttpResponse.Status?displayProperty=nameWithType / xref:System.Web.HttpResponse.StatusDescription?displayProperty=nameWithType → xref:Microsoft.AspNetCore.Http.HttpResponse.StatusCode?displayProperty=nameWithType
xref:System.Web.HttpResponse.ContentEncoding?displayProperty=nameWithType / xref:System.Web.HttpResponse.ContentType?displayProperty=nameWithType → xref:Microsoft.AspNetCore.Http.HttpResponse.ContentType?displayProperty=nameWithType
xref:System.Web.HttpResponse.ContentType?displayProperty=nameWithType → xref:Microsoft.AspNetCore.Http.HttpResponse.ContentType?displayProperty=nameWithType
xref:System.Web.HttpResponse.Output?displayProperty=nameWithType → xref:Microsoft.AspNetCore.Http.HttpResponseWritingExtensions.WriteAsync*?displayProperty=nameWithType
xref:System.Web.HttpResponse.TransmitFile*?displayProperty=nameWithType → See request features
Serving files is discussed in xref:fundamentals/request-features
xref:System.Web.HttpResponse.Headers?displayProperty=nameWithType → xref:Microsoft.AspNetCore.Http.HttpResponse.OnStarting*?displayProperty=nameWithType
Must use callback pattern to set headers before response starts
xref:System.Web.HttpResponse.Cookies?displayProperty=nameWithType → xref:Microsoft.AspNetCore.Http.HttpResponse.OnStarting*?displayProperty=nameWithType
Must use callback pattern to set cookies before response starts
Setting response headers:
public async Task Invoke(HttpContext httpContext)
{
// Set callback to execute before response starts
httpContext.Response.OnStarting(SetHeaders, state: httpContext);
// ... rest of middleware logic
}
Setting response cookies:
public async Task Invoke(HttpContext httpContext)
{
// Set callbacks to execute before response starts
httpContext.Response.OnStarting(SetCookies, state: httpContext);
httpContext.Response.OnStarting(SetHeaders, state: httpContext);
// ... rest of middleware logic
}
Choose this approach when you have extensive HttpContext usage across shared libraries or when performing an incremental migration where you want to minimize code changes.
The System.Web adapters provide a compatibility layer that allows you to use familiar xref:System.Web.HttpContext APIs in ASP.NET Core applications. This approach is particularly useful when:
System.Web.HttpContext usage patternsFor more information about System.Web adapters, see the System.Web adapters documentation.