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:
- Session save data similar to dictionary type for example keys like string and values like object.
- Data store as object in session.
- Whenever we retrieving data from session we need to type casted to its original type as data store in object.
- It is also necessary to apply null check while retrieving data from session.
- It pass values from controller to view and controller to another controller.
- Session is available across multiple request.
How to use:
Following are method or way Asp.net provide o store data in session:
- InProc
- State server
- 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 =
newList<
string>();
Session[
"var"] = myList;
For retrieving list we use this syntax
myList = (List<
string>)Session[
"var"];
Result/Conclusion:
- Session is the technique in Asp.net mvc through which we can store amount of data for an interval.
- It is the Httpstatebase class object.
- It has different mode which is mentioned in web config file
- In controller side we can write session as :
Session["Message"] = "Hello Name is Ahsan!";
- In view side we can access session as:
@Session["Message"]