Previous Thundering-Herd-Problem Database Replication & Sharding Next

Session Management and ViewData, ViewBag, and TempData in ASP.NET MVC

Here's a comprehensive breakdown of session management in ASP.NET and a comparison of ViewData, ViewBag, and TempDataβ€”three key mechanisms used to pass data between controllers and views.

πŸ—‚οΈ Session Management in ASP.NET

ASP.NET is built on the stateless HTTP protocol, so session management is crucial for maintaining user-specific data across multiple requests.

πŸ” What Is Session?

A server-side storage mechanism that retains user data across multiple requests.

  • Identified by a session ID stored in a browser cookie.
  • Commonly used for:
    • User authentication
    • Shopping carts
    • Preferences
    • Temporary data caching

🧰 Session Modes in ASP.NET

Mode Description
InProc Stores session in server memory. Fast but not durable across server restarts.
StateServer Stores session in a separate process. More durable than InProc.
SQLServer Stores session in a SQL database. Best for web farms and durability.
Custom Allows custom session state providers.
Off Disables session state.

πŸ•’ Session Behavior

  • Default timeout: 20 minutes
  • Data is cleared when session expires or browser is closed
  • Use HttpContext.Session.SetString() and GetString() in ASP.NET Core

πŸ“¦ ViewData

πŸ”Ή What It Is:

A dictionary object (ViewDataDictionary) used to pass data from controller to view.

πŸ”Ή Characteristics:

  • Stores data as key-value pairs.
  • Requires explicit casting in the view.
  • Available only during the current request.

πŸ”Ή Example:

//csharp
ViewData["Message"] = "Hello from ViewData!";
html
<h2>@ViewData["Message"]</h2>

πŸ‘œ ViewBag

πŸ”Ή What It Is:

A dynamic wrapper around ViewData.

πŸ”Ή Characteristics:

  • Easier syntax using dynamic properties.
  • Also available only during the current request.
  • Internally uses ViewData.

πŸ”Ή Example:

//csharp
ViewBag.Message = "Hello from ViewBag!";
html
<h2>@ViewBag.Message</h2>

πŸ” TempData

πŸ”Ή What It Is:

A dictionary (TempDataDictionary) used to pass data between controller actions.

πŸ”Ή Characteristics:

  • Persists data across redirects (i.e., from one request to the next).
  • Uses session internally.
  • Data is removed after it's read once (unless Keep() or Peek() is used).

πŸ”Ή Example:

csharp
TempData["Message"] = "Hello from TempData!";
return RedirectToAction("NextAction");
html
<h2>@TempData["Message"]</h2>

🧠 Summary Table

Feature ViewData ViewBag TempData
Type Dictionary Dynamic wrapper Dictionary
Scope Current request only Current request only Current + next request
Type Safety Not type-safe Not type-safe Not type-safe
Use Case Pass to view Pass to view Pass between actions
Persistence Lost on redirect Lost on redirect Retained across redirect

βœ… When to Use What

  • Use ViewData or ViewBag when passing data from controller to view in the same request.
  • Use TempData when redirecting to another action and need to pass data along.
  • Prefer ViewBag for cleaner syntax unless you need dictionary-like behavior.

πŸ“Š ViewData vs ViewBag vs TempData vs Session

Feature ViewData ViewBag TempData Session
Type Dictionary (ViewDataDictionary) Dynamic property Dictionary (TempDataDictionary) Dictionary (ISession)
Scope Current request only Current request only Current + next request (redirect) Entire user session
Type Safety Not type-safe (requires casting) Not type-safe (dynamic) Not type-safe (requires casting) Not type-safe (requires casting)
Use Case Pass data from controller to view Same as ViewData, but simpler Pass data between actions/views Persist user-specific data
Persistence Lost on redirect Lost on redirect Retained across redirect Retained until session expires
Example ViewData["Name"] = "Shiv" ViewBag.Name = "Shiv" TempData["Name"] = "Shiv" Session["Name"] = "Shiv"

βœ… Best Practices

  • Use ViewData/ViewBag for simple, short-lived data between controller and view.
  • Use TempData for redirect scenarios (e.g., success messages).
  • Use Session for user-specific data that needs to persist across multiple pages.
  • Avoid storing sensitive or large data in any of these mechanisms.
Back to Index
Previous Thundering-Herd-Problem Database Replication & Sharding Next
*