Access HttpContext from everywhere

Ok, so I totally get why you shouldn’t rely on statics for this kind of thing. Makes perfect sense. What doesn’t make sense is for such a ubiquitous thing as accessing the current user to be so hard to do in ASP.NET Core. Just look at the options in the official documentation.

The One True DI Way should just to be able to add HttpContext as a dependency anywhere you need it, right? The way to do so in ASP.NET Core without “violating” any principles (at least the way I understand them) would be to properly expose this as a scoped dependency, like so:

// Program.cs
builder.Services.AddHttpContextAccessor();
builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpContextAccessor>().HttpContext!);

Now I can just add HttpContext to my fancy primary constructors!

/kzu dev↻d