Table of Contents

HttpContextInstance

Global access to the current HttpContext without having to inject it through a dependency chain.

Features

  • Current context accessor: HttpContextInstance.Current returns the active request context from anywhere in the app
  • Static helper support: useful in extension methods and framework-level utilities that do not accept a HttpContext parameter

Behavior

var context = HttpContextInstance.Current;
var path = context.Request?.Path;

When no HTTP request is active, the framework returns a default empty context instead of null, which makes static helpers safe to call during background work, testing, or console execution.

Caveats

  • If no HTTP request is active, the returned object is a default empty HttpContext with an empty ClaimsPrincipal.
  • This is intended for code paths that need a safe default, not for forcing a real request context.
  • AddFrameworkServices() should be called during startup so that the singleton access pattern is registered correctly.
Tip

Use HttpContextInstance.Current in static helpers where you need request data but do not want to pass HttpContext through every method call.