Access the current HttpContext in ASP.NET Core

maxswitcher

I need to access current HttpContext in a static method or a utility service.

With classic ASP.NET MVC and System.Web, I would just use HttpContext.Current to access the context statically. But how do I do this in ASP.NET Core?

Kévin Chalet

HttpContext.Current doesn't exist anymore in ASP.NET Core but there's a new IHttpContextAccessor that you can inject in your dependencies and use to retrieve the current HttpContext:

public class MyComponent : IMyComponent
{
    private readonly IHttpContextAccessor _contextAccessor;

    public MyComponent(IHttpContextAccessor contextAccessor)
    {
        _contextAccessor = contextAccessor;
    }

    public string GetDataFromSession()
    {
        return _contextAccessor.HttpContext.Session.GetString(*KEY*);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Asp.net Core 5.0 does not appear to have httpContext.Session.GetString as a method when it use to

From Java

How to get HttpContext.Current in ASP.NET Core?

From Java

How to get current user in asp.net core

From Java

How to get current url in view in asp.net core 1.0

From Java

How to get the current logged in user Id in ASP.NET Core

From Java

ASP.NET Core Identity - get current user

From Java

Can't access to HttpContext.Current

From Dev

ASP.net download file Using HttpContext.Current.Response.TransmitFile in ASP.NET

From Dev

ASP.NET: The difference between page.Session and HttpContext.Current.Session

From Dev

Why is HTTPContext.Current.Session null using SignalR 2.x libraries in a ASP .Net MVC application?

From Dev

Async task ASP.net HttpContext.Current.Items is empty - How do handle this?

From Dev

How to access HttpContext inside a unit test in ASP.NET 5 / MVC 6

From Dev

Alternative to HttpContext.Current.Items in ASP.NET Web Api

From Dev

where to place HttpContext.Current.RewritePath in asp.net 5?

From Dev

Asp.Net Core Middleware service dependent on current User

From Dev

Why I can't use HttpContext or HttpCookie? (Asp.Net Core 1.0)

From Dev

Access the current HttpContext from a ILogger

From Dev

ASP.NET Core - The name 'JsonRequestBehavior' does not exist in the current context

From Dev

Can't access or authorize ASP.Net Web API after setting Thread and HttpContext principal

From Dev

HttpContext .NET core saving instance in Middleware

From Dev

Access Denied in Asp .Net Core by Claims Authorization

From Dev

COM visible .NET class cannot get System.Web.HttpContext.Current when used in classic asp

From Dev

why my HttpContext.Application variable cannot be access through different Actions in ASP.NET MVC

From Dev

Alternative to HttpContext.Current.Items in ASP.NET Web Api

From Dev

How get current login user in ViewComponent Asp.Net Core

From Dev

auth0 authentication issue: HttpContext.Current is null. This code path is only valid when in the execution context of ASP.NET

From Dev

How to access cookie asp.net core

From Dev

Send ASP.NET MVC HttpContext to Web Api HttpContext

From Dev

Is HttpContext.Current.Items["SameKey"] unique for each call made to the ASP.NET WebApi?

Related Related

  1. 1

    Asp.net Core 5.0 does not appear to have httpContext.Session.GetString as a method when it use to

  2. 2

    How to get HttpContext.Current in ASP.NET Core?

  3. 3

    How to get current user in asp.net core

  4. 4

    How to get current url in view in asp.net core 1.0

  5. 5

    How to get the current logged in user Id in ASP.NET Core

  6. 6

    ASP.NET Core Identity - get current user

  7. 7

    Can't access to HttpContext.Current

  8. 8

    ASP.net download file Using HttpContext.Current.Response.TransmitFile in ASP.NET

  9. 9

    ASP.NET: The difference between page.Session and HttpContext.Current.Session

  10. 10

    Why is HTTPContext.Current.Session null using SignalR 2.x libraries in a ASP .Net MVC application?

  11. 11

    Async task ASP.net HttpContext.Current.Items is empty - How do handle this?

  12. 12

    How to access HttpContext inside a unit test in ASP.NET 5 / MVC 6

  13. 13

    Alternative to HttpContext.Current.Items in ASP.NET Web Api

  14. 14

    where to place HttpContext.Current.RewritePath in asp.net 5?

  15. 15

    Asp.Net Core Middleware service dependent on current User

  16. 16

    Why I can't use HttpContext or HttpCookie? (Asp.Net Core 1.0)

  17. 17

    Access the current HttpContext from a ILogger

  18. 18

    ASP.NET Core - The name 'JsonRequestBehavior' does not exist in the current context

  19. 19

    Can't access or authorize ASP.Net Web API after setting Thread and HttpContext principal

  20. 20

    HttpContext .NET core saving instance in Middleware

  21. 21

    Access Denied in Asp .Net Core by Claims Authorization

  22. 22

    COM visible .NET class cannot get System.Web.HttpContext.Current when used in classic asp

  23. 23

    why my HttpContext.Application variable cannot be access through different Actions in ASP.NET MVC

  24. 24

    Alternative to HttpContext.Current.Items in ASP.NET Web Api

  25. 25

    How get current login user in ViewComponent Asp.Net Core

  26. 26

    auth0 authentication issue: HttpContext.Current is null. This code path is only valid when in the execution context of ASP.NET

  27. 27

    How to access cookie asp.net core

  28. 28

    Send ASP.NET MVC HttpContext to Web Api HttpContext

  29. 29

    Is HttpContext.Current.Items["SameKey"] unique for each call made to the ASP.NET WebApi?

HotTag

Archive