The Core application accesses HttpContext through the IHttpContextAccessor interface and its default implementation HttpContextAccessor. IHttpContextAccessor is only necessary if you need to access the HttpContext within the service.
Using HttpContext with Razor Pages
Razor Pages PageModel exposes the HttpContext property:
public class AboutModel : PageModel { public string Message { get; set; } public void OnGet() { Message = ; } }
Using HttpContext via Razor View
The Razor view exposes the HttpContext directly through the properties on the view. The following example uses Windows authentication to retrieve the current username in an intranet application:
@{ var username = ; ... }
Using HttpContext via controller
Controller exposes properties:
public class HomeController : Controller { public IActionResult About() { var pathBase = ; ... return View(); } }
Using HttpContext via middleware
When using a custom middleware component, HttpContext is passed to the Invoke or InvokeAsync method, which is accessible after middleware configuration:
public class MyCustomMiddleware { public Task InvokeAsync(HttpContext context) { ... } }
Use HttpContext with custom components
For other frameworks and custom components that require access to HttpContext, it is recommended to use the built-in dependency injection container to register the dependencies. The dependency injection container provides an IHttpContextAccessor to any class, so that the class declares it as a dependency in its own constructor:
public void ConfigureServices(IServiceCollection services) { (); (); <IUserRepository, UserRepository>(); }
In the following example:
- UserRepository declares its dependency on IHttpContextAccessor.
- When the dependency injection container parses the dependency chain and creates a UserRepository instance, the dependency is injected.
public class UserRepository : IUserRepository { private readonly IHttpContextAccessor _httpContextAccessor; public UserRepository(IHttpContextAccessor httpContextAccessor) { _httpContextAccessor = httpContextAccessor; } public void LogCurrentUser() { var username = _httpContextAccessor.; (username); } }
Accessing HttpContext from background thread
HttpContext is not thread-safe. Reading or writing properties of HttpContext outside of processing requests may result in a NullReferenceException.
To perform background work securely using HttpContext data:
- Copy the required data during the request processing.
- Pass the copied data to the background task.
To avoid unsafe code, do not pass HttpContext to methods that perform background work. Instead, pass the required data. In the following example, call SendEmailCore to start sending emails. Pass correlationId to SendEmailCore, not HttpContext. Code execution does not wait for SendEmailCore to complete:
public class EmailController : Controller { public IActionResult SendEmail(string email) { var correlationId = ["x-correlation-id"].ToString(); _ = SendEmailCore(correlationId); return View(); } private async Task SendEmailCore(string correlationId) { ... } }
This is the article about the methods and steps of accessing HttpContext in Core5.0. For more related Core accessing HttpContext, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!