In this Article, I describe what is Session and how to use session in asp.net mvc. Session is helpful to access or pass a data from controller to View or one View to controller that mean we can access a session value anywhere in application until user browser is open.
Syntax
Session["SessionName"]
Example
Session["Success"]= "Welcome to www.Infinetsoft.com";
HomeController
public ActionResult Index()
{
Session["Message1"] = "First Message";
return RedirectToAction("Index1");
}
public ActionResult Index1()
{
string str = Session["Message1"].ToString();
Session["Message1"] = "Second Message";
return View(Index);
}
Index:View
@{
ViewBag.Title = "Index";
}
@Session["Message1"];
Description
Here I demonstrate with small example, When I build a application Index method executed first and it take First Message to session[“Message1”] and index method is redirect to index1. In Index1 method , I change the session value and it take Second Message in Session[“Message1”].
Post your comments / questions
Recent Article
- ModuleNotFounEerror:No module named celery in Django Project
- How to get domain name information from a Domain using Python
- ModulenotFoundError: no module named 'debug_toolbar' -SOLUTION
- How to create superuser in django project hosted in cPanel without terminal
- CSS & images not loading in django admin | cpanel without terminal
- Could not build wheels for mysqlclient, which is required to install pyproject.toml-based projects
- How to sell domain name on Godaddy (2023)
- TemplateSyntaxError at / Could not parse the remainder: ' + 1' from 'forloop.counter0 + 1'
Related Article