liveBook · Manning

TempData

As explained in ASP.NET Core in Action, 3e

TempData is an ASP.NET Core temporary-data mechanism that stores small values for retrieval during a subsequent request, commonly using cookies.

TempData is a temporary dictionary for carrying small values across requests. It is especially useful when a form submission redirects to another page and that page needs a short-lived message or value. section 15.4.1 section 17.2.3

Use TempData when:

  • A POST handler redirects to a GET handler in the POST-REDIRECT-GET pattern. section 17.2.3
  • The value is small and should be available only temporarily. section 15.4.1
  • You need dictionary entries with string keys rather than ordinary PageModel properties. section 17.2.3

By default, ASP.NET Core stores TempData in an encrypted cookie. Other storage options are available. For ordinary data displayed by one page, prefer PageModel properties; use TempData for short-lived values that must survive a redirect. section 17.2.3

Figure 15.3 A typical POST, REDIRECT, GET flow through a website. A user sends their shopping basket to a checkout page, which validates its contents and redirects to a payment page without the user’s having to change the URL manually.
CH15_F03_Lock3

Follow the POST-REDIRECT-GET flow to see where temporary data is needed between the submitting page and the destination page. section 15.4.1

How ASP.NET Core in Action, 3e covers it

How deep it goes
Defines dictionary behavior, request lifetime, default storage, and alternatives but does not provide implementation code.
Examples
Uses an e-commerce checkout redirect and contrasts TempData with PageModel properties and ViewData.
What it assumes
Assumes familiarity with Razor Pages or views, page handlers, requests, redirects, and PageModel data.
What you can do after
Apply it via ASP.NET Core's TempData mechanism when a redirect must carry a small temporary value.
How it connects
Connects TempData with Razor Pages, page handlers, redirects, PageModel properties, ViewData, cookies, and POST-REDIRECT-GET.

Also explained in

Compare how 2 books explain TempData →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.