when is better to use session to maintain class or ptoperties of class?

Mohammad Ali Echreshavi 101 Reputation points
2023-06-05T10:41:14.89+00:00

hi,

i will be glad if any one can explain for me what difference between bellow code or when is better i used session,

and if i want use session , which is better? keep whole class or all the feilds of class

and that in temsof efficiency, this work is correct?

   public String Name
        {
            get
            {
                return HttpContext.Current.Session["Report_Name"] as String;
            }
            set
            {
                HttpContext.Current.Session["Report_Name"] = value;
            }
        }

and

   public String Name {get ; set;}
        

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,414 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,633 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 27,656 Reputation points
    2023-06-05T11:37:24.7033333+00:00

    As far as I can tell you're asking how Session works.

    Session persists user data in the web server's memory by a key. The key is stored in an HTTP cookie. On each request the browser sends the HTTP cookie to the web server. The Session API uses the key to find and load the user's Session.

    Please read the official documentation.

    ASP.NET Session State Overview

    The difference between the first code block that uses Session and the second is the second code block does not persist the "Name" property between requests. The first code block saves the "Name" property between requests.

    A couple of points, the first code block is not well designed. Session is volatile as it exists in server memory. Always check is Session exists before trying to use a Session value. Again, see the docs.

    In terms of architecture, session exists in server memory. In a load balanced environment (multiple servers) only one server could have the Session state.

    0 comments No comments

0 additional answers

Sort by: Most helpful