How to get and set session in Asp.net mvc?

  By Ahsan   Posted on June-24-2022 

How to get and set session in Asp.net mvc?

In this Article we will learn about what is session and what session management is in Asp.net mvc:

What is Session?

Firstly we can understand what session .the word session meaning is that an interval of time or a period of time. In Asp.Net mvc Session can be defined as the session is temporary memory location where we can hold data in small amount for certain period or interval of time during the visit of the user any website.

Session is the HttpSessionStateBase class object. In simple session is derived form the HttpSessionStateBase and is used for keep data.As we can say that website is stateless it help us to manage the state of the website across the each request in Asp.net mvc razor.

How it works:

Following points explain the working of the session methodology:

  1. Session save data similar to dictionary type for example keys like string and values like object.
  2. Data store as object in session.
  3. Whenever we retrieving data from session we need to type casted to its original type as data store in object.
  4. It is also necessary to apply null check while retrieving data from session.
  5. It pass values from controller to view and controller to another controller.
  6. Session is available across multiple request.

 How to use:

Following are method or way Asp.net provide o store data in session:

  1. InProc
  2. State server
  3. SQL server

 

InProc:

InProc mode is the default Session state modes, in this mode all the session data store in the memory of the web server that we use.

 State Server:

State server also called the out Proc mode. In this mode all the session data are store in separate memory called Asp.net services. We can maintain the session data even when application process restarted.

SQL Server:

In this mode Session data store in SQL server Database. We can maintain the session data even when application process restarted.

Keep in mind we can specified the state of session in web.config, default mode is “InProc”, here we can also define the duration time as show in below code:

 <system.web>

<sessionState mode="InProc" timeout="5" />

</system.web>

Example:

Following are the examples shows the how a value set in session, we store the string in the object then shoe in the view.

In Controller:

This example show how to add string in session:

public class MyFirstSessionController : Controller

{

    // GET: First

    public ActionResult Index()

    {

        Session["Message"] = "Hello Name is Ahsan!";

        return View();

    }

}

 

In above mentioned example we make a controller name as the MyFirstSession. After that we make an action method name as “Index”. In this action method we define our session as firsr we write session the write square brackets with in its example message name which is equal to the string.

In View:

<html>

<head>

    <meta name="viewport" content="width=device-width"/>

    <title>Index</title>

</head>

<body>

    <div>

        @Session["Message"]

    </div>

</body>

</html>

 

In view side we write our session code as above mention for session we write “@” sign first then write Session after that we wirte the same name as in declare in controller message.

This example show how to add integer value in session:

public ActionResult Index(string id, string co)

{

    Customer cust = new Customer();

   

    cust.Type=2;

 

    Session["customer"] = cust;

 

    return View(customer);

}

 

This example show how to assigning list object value in session

List<string> myList = new List<string>();
Session["var"] = myList;

 

For retrieving list we use this syntax

myList = (List<string>)Session["var"];

 

Result/Conclusion:

  1. Session is the technique in Asp.net mvc  through which we can store amount of data for an interval.
  2. It is the Httpstatebase class object.
  3. It has different mode which is mentioned in web config file
  4. In controller side we can write session as :

                     Session["Message"] = "Hello Name is Ahsan!";

  1. In view side we can access session as:

        @Session["Message"]

By      24-Jun-2022 Views  73



Comments


Your Comments

You may also read following recent articles