In this article I will explain why HttpContext.Current not working in ASP.Net Core Razor Pages and what is the alternative solution for using the functionality in ASP.Net Core Razor Pages.
Microsoft has permanently removed HttpContext.Current property from .Net Core and introduced a new interface IHttpContextAccessor for .Net Core.
What is IHttpContextAccessor
The IHttpContextAccessor is an interface for .Net Core for accessing HttpContext property.
This interface needs to be injected as dependency in the IndexModel and then later used throughout the IndexModel.
This interface allows us to access the HttpContext property which in turn provides access to Request collection and also the Response property.
Startup class Configuration
You will need to add the AddHttpContextAccessor function in the ConfigureServices method of Startup class as shown below.
public void ConfigureServices(IServiceCollection services)
You will need to import the following namespace.
using Microsoft.AspNetCore.Http;
Using IHttpContextAccessor in Razor PageModel (Code-Behind)
In the below example, the IHttpContextAccessor is injected in the constructor of the IndexModel class and assigned to the private property Accessor and later it used to access the HttpContext property.
public class IndexModel : PageModel
    private IHttpContextAccessor Accessor;
    public IndexModel(IHttpContextAccessor _accessor)
        this.Accessor = _accessor;
    Public void OnGet()
        HttpContext context = this.Accessor.HttpContext;
Using HttpContext in ASP.Net Core Razor Pages
Download Code