LP13
LP13

Reputation: 34129

How to get HttpContext in configure method in asp.net core?

I have asp.net core application and I want current HttpContext in configure method. So i am passing IHttpContextAccessor to configure method. Something like below

 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, 
                      IApplicationLifetime appLifetime, IServiceProvider services, 
                      IHttpContextAccessor httpContextAccessor)
{    
           var request = httpContextAccessor.HttpContext.Request;    
}

However httpContextAccessor.HttpContext is always null

Upvotes: 1

Views: 2339

Answers (1)

Henk Mollema
Henk Mollema

Reputation: 46591

This does not work because there is no HTTP-request (as R.Richards pointed out) when your application starts. Configure is called by the runtime when the application starts, not when the first request is handled.

Please take a look at the documentation about application startup: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/startup.

Upvotes: 5

Related Questions