locked
'HttpContext' does not contain a definition for 'Current' RRS feed

  • Question

  • User2056817590 posted

    public static string pier
            {
                get
                {
                    initPier();
                    return (string)HttpContext.Session["pierSelected"];
                }
                set
                {
                    HttpContext.Current.Session["pierSelected"] = value;
                }
            }

    Tuesday, April 6, 2021 12:07 PM

All replies

  • User475983607 posted

    public static string pier
            {
                get
                {
                    initPier();
                    return (string)HttpContext.Session["pierSelected"];
                }
                set
                {
                    HttpContext.Current.Session["pierSelected"] = value;
                }
            }

    You've asked a series of question today which indicate you are migrating to .NET 5 but have not taken the time to learn .NET 5 basics.   I recommend reading the migration guide and fundamentals guide rather than guessing how .NET 5 works. 

    Http Context

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-context?view=aspnetcore-5.0

    Session

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-5.0

    Tuesday, April 6, 2021 12:16 PM
  • User1120430333 posted

    All About Sessions In ASP.NET Core (c-sharpcorner.com)

    Maybe, the link will help you.

    Tuesday, April 6, 2021 12:19 PM
  • User-474980206 posted

    You have run into one of the major porting to asp.net core issues. Due to asp.net cores new threading model, HttpContext.Current and all of its variants are not implemented. This was commonly used in old asp.net for static methods to access session or other context objects. You will need to refactor your code to pass this objects to the static methods.

    Note: you should  look at using DI to replace the static methods. Also session is async and works different in core. Read the docs.

    Tuesday, April 6, 2021 2:33 PM
  • User1312693872 posted

    Hi,kalvakrishna

    HttpContext.Current.Session

    This does not support asp.net core, in core you can use session like this:

    HttpContext.Session.SetString("session name","session string");
    var priceDetails = HttpContext.Session.GetString("session name");

    Don't forget to add

    services.AddSession();

    in ConfigureServices and add

    app.UseSession();

    in Configure first. More details, you can check the official link:

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-5.0

    Best Regards,

    Jerry Cai

    Wednesday, April 7, 2021 3:28 AM
  • User2056817590 posted

    Getting following exception if I used session in static property 

    An object reference is required for the non-static field, method, or property 'HttpContext.Session'

    Wednesday, April 7, 2021 5:48 AM
  • User475983607 posted

    Getting following exception if I used session in static property 

    An object reference is required for the non-static field, method, or property 'HttpContext.Session'

    This is the expected result and clearly explained above.   The standard pattern in Core is dependency injection.  Create a standard service and inject Session into the service via the constructor.  From there you can expose Session variables via dot syntax rather than using strings in a controller or razor page.  Please read the docs...

    Wednesday, April 7, 2021 12:33 PM