From 07e2dc83ff6a59de94da0751e5f34a50e346e7cf Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Sat, 21 Jul 2018 15:48:34 -0700 Subject: [PATCH] =?UTF-8?q?Added=20documentation=20for=20IHttpContextAcces?= =?UTF-8?q?sor=20to=20describe=20its=20use=20in=20c=E2=80=A6=20(#6489)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aspnetcore/fundamentals/http-context.md | 117 ++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 aspnetcore/fundamentals/http-context.md diff --git a/aspnetcore/fundamentals/http-context.md b/aspnetcore/fundamentals/http-context.md new file mode 100644 index 0000000000..980ce4daea --- /dev/null +++ b/aspnetcore/fundamentals/http-context.md @@ -0,0 +1,117 @@ +--- +title: Access HttpContext in ASP.NET Core +author: coderandhiker +description: Learn how to access HttpContext in ASP.NET Core. +ms.author: riande +ms.custom: mvc +ms.date: 07/20/2018 +uid: fundamentals/httpcontext +--- +# Access HttpContext in ASP.NET Core + +ASP.NET Core apps access the `HttpContext` through the [IHttpContextAccessor](/dotnet/api/microsoft.aspnetcore.http.ihttpcontextaccessor) interface and its default implementation [HttpContextAccessor](/dotnet/api/microsoft.aspnetcore.http.httpcontextaccessor). + +::: moniker range=">= aspnetcore-2.0" + +## Use HttpContext from Razor Pages + +The Razor Pages [PageModel](/dotnet/api/microsoft.aspnetcore.mvc.razorpages.pagemodel) exposes the [HttpContext](/dotnet/api/microsoft.aspnetcore.mvc.razorpages.pagemodel.httpcontext) property: + +```csharp +public class AboutModel : PageModel +{ + public string Message { get; set; } + + public void OnGet() + { + Message = HttpContext.Request.PathBase; + } +} +``` + +::: moniker-end + +## Use HttpContext from a controller + +Controllers expose the [ControllerBase.HttpContext](/dotnet/api/microsoft.aspnetcore.mvc.controllerbase.httpcontext) property: + +```csharp +public class HomeController : Controller +{ + public IActionResult About() + { + var pathBase = HttpContext.Request.PathBase; + // Do something with the PathBase. + + return View(); + } +} +``` + +## Use HttpContext from middleware + +When working with custom middleware components, `HttpContext` is passed into the `Invoke` or `InvokeAsync` method and can be accessed when the middleware is configured: + +```csharp +public class MyCustomMiddleware +{ + public Task InvokeAsync(HttpContext context) + { + // Middleware initialization optionally using HttpContext + } +} +``` + +## Use HttpContext from custom components + +For other framework and custom components that require access to `HttpContext`, the recommended approach is to register a dependency using the built-in [dependency injection](xref:fundamentals/dependency-injection) container. The dependency injection container supplies the `IHttpContextAccessor` to any classes that declare it as a dependency in their constructors. + +::: moniker range=">= aspnetcore-2.1" + +```csharp +public void ConfigureServices(IServiceCollection services) +{ + services.AddMvc() + .SetCompatibilityVersion(CompatibilityVersion.Version_2_1); + services.AddHttpContextAccessor(); + services.AddTransient(); +} +``` + +::: moniker-end + +::: moniker range="<= aspnetcore-2.0" + +```csharp +public void ConfigureServices(IServiceCollection services) +{ + services.AddMvc(); + services.AddSingleton(); + services.AddTransient(); +} +``` + +::: moniker-end + +In the preceding example: + +* `UserRepository` declares its dependency on `IHttpContextAccessor`. +* The dependency is supplied when dependency injection resolves the dependency chain and creates an instance of `UserRepository`. + +```csharp +public class UserRepository : IUserRepository +{ + private readonly IHttpContextAccessor _httpContextAccessor; + + public UserRepository(IHttpContextAccessor httpContextAccessor) + { + _httpContextAccessor = httpContextAccessor; + } + + public void LogCurrentUser() + { + var username = _httpContextAccessor.HttpContext.User.Identity.Name; + service.LogAccessRequest(username); + } +} +```