Net 6 using session in a custom class

Jim Whitaker 66 Reputation points
2021-12-16T04:17:30.797+00:00

I can easily work with session in a page model like:

            string SessionKeyName = "logged";
            if (string.IsNullOrEmpty(HttpContext.Session.GetString(SessionKeyName)))
            {
                HttpContext.Session.SetString(SessionKeyName, "notlogged");
                //ViewData["mt"] = "notlogged";
                Response.Redirect("./login");
            }

But if I try to use session in a custom class I get an error:

Severity Code Description Project File Line Suppression State
Error CS0120 An object reference is required for the non-static field, method, or property 'HttpContext.Session' RazorPagesMovie C:\Users\Owner\source\repos\RazorPagesMovie\RazorPagesMovie\Helpers\Chk.cs 20 Active

How would I implement session in a custom class?

Developer technologies ASP.NET ASP.NET Core
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2021-12-17T17:33:46.683+00:00

    it would be simpler to just pass session to the class:

    using Microsoft.AspNetCore.Http;
     using Microsoft.AspNetCore.Mvc;
     using Microsoft.AspNetCore.Session;
    
    
     namespace RazorPagesMovie.Helpers
     {
         public class Chk
         {
             ISession session;
    
             public Chk(ISession session)
             {
                 this.session = session;
             }
    
             public void Foo()
             {
                 session?.SetString("Name", "Bobby");
                 session?.SetInt32("Age", 773);
             }
    
             public void getFoo()
             {
                 session?.GetString("Name");
                 session?.SetInt32("Age", 773);
             }
         }
     }
    

    in a page you create

    Chk chk = new Chk(HttpContext.Session);

    or create a session extension class

    public static class SessionExtensions
    {
         public static string? GetFoo (this ISession session) => return session["foo"];
         public static void SetFoo(this ISession session, string value) => session["foo"] = value; 
    }
    

    and use

    var foo = HttpContext.Session?.GetFoo();

    1 person found this answer helpful.
    0 comments No comments

9 additional answers

Sort by: Most helpful
  1. Jim Whitaker 66 Reputation points
    2021-12-17T18:45:14.977+00:00

    Bruce-SqlWork passing session worked, however I had to modify like:

       public class Chk
        {
            ISession session;
    
            public Chk(ISession session)
            {
                this.session = session;
            }
    
            public void Foo()
            {
                session?.SetString("Name", "Bobby");
                session?.SetInt32("Age", 773);
            }
    
            public dynamic? getFoo(string rvalue)
            {
                if (string.IsNullOrEmpty(session.GetString(rvalue)))
                {
                    session.SetString("logged", "notlogged");
                    return session?.GetString(rvalue);
                }
                else
                {
                    return session?.GetString(rvalue);
                }
    
                //return "na";
    
    
                //session?.GetString(rvalue);
                //session?.SetInt32("Age", 773);
            }
        }
    }
    

    The ide VS 2022 complained about the void and a return.

    Is there any way now to make the methods static?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.