Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Session Management in ASP.NET MVC

DZone's Guide to

Session Management in ASP.NET MVC

In this article, a developer discusses three different ways to deal with session management in ASP.NET MVC and provides some code for each.

· Web Dev Zone ·
Free Resource

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

Difference Between ViewData, ViewBag, and TempData With Code Examples

In this article, I will explain, with an example, the similarities and differences between ViewDataViewBag, and TempData in ASP.NET MVC.

ViewDataViewBag and TempData are used for transferring data and objects from the Controller to the View or from one Controller to another in ASP.NET MVC.

ViewData

  1. ViewData is derived from the ViewDataDictionary class and is basically a Dictionary object, i.e. it has keys and values where keys are strings and Values are objects.

  2. Data is stored as an Object inViewData.

  3. While retrieving data, the data needs to be Type Cast to its original type, as it will be stored as an object. ViewData also requires NULL checks while retrieving data.

  4. ViewData is used for passing a value from the Controller to the View.

  5. ViewData is available only for Current Requests. It will be destroyed upon redirection.

Example

In the below example, a string value is set in the ViewData object in the Controller and it is then displayed in the View.

Controller

public class FirstController: Controller {
 // GET: First
 public ActionResult Index() {
  ViewData["Message"] = "Hello Sultan!";
  return View();
 }
}

View

<html>
 <head>
  <meta name="viewport" content="-width"/>
  <title>Index</title>
 </head>
<body>
 <div>
  @ViewData["Message"]
  </div>
 </body>
</html>

ViewBag

  1. ViewBag is a Wrapper built around ViewData.

  2. ViewBag is a dynamic property and it makes use of C# 4.0's dynamic features.

  3. While retrieving data, there is no need for Type Casting the data.

  4. ViewBag is used for passing a value from the Controller to the View.

  5. ViewBagis available only for Current Requests. It will be destroyed upon redirection.

Example

In the below example, a string value is set in the ViewBag object in the Controller and it is then displayed in the View.

Controller

public class FirstController: Controller {
 // GET: First
 public ActionResult Index() {
  ViewBag.Message = "Hello Sultan!";
  return View();
 }
}

View

<html>
 <head>
  <meta name="viewport" content="-width"/>
  <title>Index</title>
 </head>
<body>
 <div>
  @ViewBag.Message
 </div>
</body>
</html>

TempData

  1. TempData is derived from the TempDataDictionary class and is basically a Dictionary object, i.e. keys and values where keys are string while values will be objects.

  2. Data is stored as an object in TempData.

  3. While retrieving, the data it needs to be Type Casted to its original type as the data is stored as objects and it also requires NULL checks while retrieving data.

  4. TempData can be used for passing a value from the Controller to the View and also from the Controller to another Controller.

  5. TempData is available for current and subsequent requests. It will not be destroyed on redirection.

Example

In the below example, a string value is set in the TempData object in the Controller and it is redirected to another Controller and, finally, it is displayed in the View.

First Controller

public class FirstController: Controller {
  // GET: First
  public ActionResult Index() {
   TempData["Message"] = "Hello Sultan!";
   return new RedirectResult(@ "~\Second\");
   }
  }

Second Controller

public class SecondController: Controller {
 // GET: Second
 public ActionResult Index() {
  return View();
 }
}

View of Second Controller

<html>
 <head>
    <meta name="viewport" content="-width"/>
    <title>Index</title>
 </head>
<body>
 <div>
  @TempData["Message"];
 </div>
</body>
</html>

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

Topics:
asp.net ,session management ,web dev ,asp.net mvc

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}