In ASP.NET 5, application state can be managed in a variety of ways, depending on when and how the state is to be retrieved.
Here we will be talking about session state configuration and uses.
Please download the sample application from below links.
https://github.com/kalpeshsatasiya/aspnet5session
In Asp.Net 5 for the session need to add session package that provides middleware for managing session state
For package installation, Just need to include reference of following packages in your project.json file.
Once the package is restored, Now time to configure session on startup.cs class page.
For session configuration first need to setup cache because of a session is built on top of IDistributedCache.
For Cache and Session configuration need to add following statements on ConfigureServices method.
And one more statement needs to add on Configure method.
You can use easy to get and set data on the session to primitive data type. Complex object not directly store on a session. The session will work with byte[] no object.
If you want to store object data into session then it will require serialize the object to a byte[] in order to store them.
Hope you help for understanding session state in asp.net 5 and keep reading.
Here we will be talking about session state configuration and uses.
Please download the sample application from below links.
https://github.com/kalpeshsatasiya/aspnet5session
In Asp.Net 5 for the session need to add session package that provides middleware for managing session state
For package installation, Just need to include reference of following packages in your project.json file.
"Microsoft.AspNet.Session": "1.0.0-rc1-final", "Microsoft.Extensions.Caching.Memory": "1.0.0-rc1-final"
Once the package is restored, Now time to configure session on startup.cs class page.
For session configuration first need to setup cache because of a session is built on top of IDistributedCache.
For Cache and Session configuration need to add following statements on ConfigureServices method.
services.AddCaching(); services.AddSession();
And one more statement needs to add on Configure method.
app.UseSession();Once a session is installed and configured, you refer to it via HttpContext and use the session.
///Set Session values HttpContext.Session.SetInt32("UserId", 1); HttpContext.Session.SetString("UserName", "Kalpesh"); ///Get Session Values ViewBag.UserId = HttpContext.Session.GetInt32("UserId"); ViewBag.UserName = HttpContext.Session.GetString("UserName");
You can use easy to get and set data on the session to primitive data type. Complex object not directly store on a session. The session will work with byte[] no object.
If you want to store object data into session then it will require serialize the object to a byte[] in order to store them.
Hope you help for understanding session state in asp.net 5 and keep reading.
0 comments:
Post a Comment