HttpContext.Current is the most convenient thing in classic ASP.NET and one of the most expensive things to carry into .NET 8. It does not exist in ASP.NET Core. Every class that reads it is tied to System.Web, and every project that contains such a class can't target netstandard2.0 or .NET 8. In a typical in-house application it isn't confined to the web project either. It turns up in service classes, repositories, logging helpers and "utility" libraries, because it was always there and always worked.
This article is about removing it in a way that lets you keep shipping on .NET Framework while you do it.
Why it's worse than it looks
The problem isn't the call itself. It's what the call does to your project graph. Suppose Company.Services has one helper that reads the current user's culture from HttpContext.Current.Request. That project now references System.Web. So does everything that depends on Company.Services: your Windows service, your import tool, your test projects. None of them can move until that one helper is gone.
That is why Tessera reports the reach of each blocker, not just the count: eight calls in a leaf project are an afternoon, while eight calls in a core library can hold back the whole solution.
Step 1: find every use
A text search for HttpContext.Current finds most of them, but not all:
HttpContext.Current.Session,.Items,.User,.Request,.Server.MapPathare all the same dependency.HttpContextBase,HttpRequestBase,HttpSessionStateBaseandHttpContextWrapperare System.Web types too, even when they are injected properly.System.Web.HttpRuntime.CacheandHttpRuntime.AppDomainAppPathare close cousins.- Fully qualified calls (
System.Web.HttpContext.Current) don't match a search that assumes ausing.
Tessera parses every file with Roslyn and reports HttpContext.Current per member access and the System.Web abstractions per file, with the project, file and line for each, so the list is complete rather than "what grep found".
Step 2: put an interface in front of what you actually use
Look at what the code reads, not at HttpContext itself. In most applications it comes down to a handful of things: the current user, a per-request bag of values, the session, the client's IP or user agent, and mapping a path. Give each one a small interface in a library that has no reference to System.Web:
public interface IRequestInfo
{
string? UserAgent { get; }
string? ClientIp { get; }
}
public interface IRequestItems
{
T? Get<T>(string key);
void Set<T>(string key, T value);
}
Then change the callers to take the interface through their constructor. This is the slow part, and it is worth doing one caller at a time, each change going to production on .NET Framework as usual.
nopCommerce 3.90 shows the pattern half-finished. Its PayPal Standard plugin sets the user agent of an outgoing request like this:
req.UserAgent = HttpContext.Current.Request.UserAgent;
The same codebase already has an IWebHelper interface for exactly this kind of request information. Moving the plugin to it removes the last System.Web dependency from that class.
Step 3: implement it twice
While the application still runs on .NET Framework, the implementation lives in the web project and wraps System.Web:
// .NET Framework web project
public sealed class SystemWebRequestInfo : IRequestInfo
{
public string? UserAgent => HttpContext.Current?.Request.UserAgent;
public string? ClientIp => HttpContext.Current?.Request.UserHostAddress;
}
When the web layer moves to ASP.NET Core, you write the second implementation over IHttpContextAccessor. The libraries don't change at all:
// ASP.NET Core web project
public sealed class AspNetCoreRequestInfo(IHttpContextAccessor accessor) : IRequestInfo
{
public string? UserAgent => accessor.HttpContext?.Request.Headers.UserAgent;
public string? ClientIp => accessor.HttpContext?.Connection.RemoteIpAddress?.ToString();
}
// Program.cs
builder.Services.AddHttpContextAccessor();
builder.Services.AddScoped<IRequestInfo, AspNetCoreRequestInfo>();
Once the libraries only know the interfaces, they can be retargeted to netstandard2.0, which both .NET Framework 4.7.2+ and .NET 8 can use. That is the step that turns one big migration into several small ones.
Things that bite
Background work. HttpContext.Current is null on threads that don't serve a request: scheduled tasks, Task.Run, timers. Code that silently relied on it falls back to defaults there, and nopCommerce handles this explicitly with a FakeHttpContext. When you introduce the interface, decide what each value means outside a request and make it explicit, rather than reproducing the null.
Session. ASP.NET Core session is opt-in, needs a cache behind it, stores only bytes (with helpers for strings and numbers), and isn't locked per request the way classic session is. Code that stores objects in Session needs serialisation, and code that relied on the lock for consistency needs another way. If session use is widespread, count it separately. It is often the largest single item behind this blocker.
HttpContext.Items as a request cache. This one ports easily, since ASP.NET Core has HttpContext.Items too, but only if it sits behind an interface like IRequestItems above. A scoped service in the container is often a cleaner replacement.
Static helpers. Static classes that read HttpContext.Current can't take constructor parameters. Either turn them into injected services, or as a stop-gap give them a static accessor that is set once at start-up. The stop-gap keeps the migration moving, but it's worth removing later.
How long it takes
Per call site, an hour or two is usual once the interfaces exist: change the constructor, update whoever creates the class, test. The interfaces themselves are a day or two. What decides the total is less the number of calls than how deep they sit: calls inside a shared core library come first, because everything else is waiting for them.
If you want that list for your own solution, with the reach of each project, Tessera gives it to you in a few seconds, offline.