> For the complete documentation index, see [llms.txt](https://dailyjournal.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dailyjournal.gitbook.io/notes/web-frameworks/asp.net-core/fundamentals/session-and-state-management.md).

# Session & State Management

HTTP is a stateless protocol. By default, HTTP requests are independent messages that don't retain user values. In ASP.NET Core, state can be stored using several approaches.

<table><thead><tr><th width="207">Storage approach</th><th>Storage mechanism</th></tr></thead><tbody><tr><td><a href="https://learn.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-7.0#cookies">Cookies</a></td><td>HTTP cookies. May include data stored using server-side app code.</td></tr><tr><td><a href="https://learn.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-7.0#session-state">Session state</a></td><td>HTTP cookies and server-side app code</td></tr><tr><td><a href="https://learn.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-7.0#tempdata">TempData</a></td><td>HTTP cookies or session state</td></tr><tr><td><a href="https://learn.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-7.0#query-strings">Query strings</a></td><td>HTTP query strings</td></tr><tr><td><a href="https://learn.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-7.0#hidden-fields">Hidden fields</a></td><td>HTTP form fields</td></tr><tr><td><a href="https://learn.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-7.0#httpcontextitems">HttpContext.Items</a></td><td>Server-side app code</td></tr><tr><td><a href="https://learn.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-7.0#cache">Cache</a></td><td>Server-side app code</td></tr></tbody></table>

## `TempData`

This property stores data until it's read in another request. The `Keep(String)` and `Peek(string)` methods can be used to examine the data without deletion at the end of the request. `TempData` is:

* Useful for redirection when data is required for more than a single request.
* Implemented by `TempData` providers using either cookies or session state.

{% tabs %}
{% tab title="First Tab" %}

```csharp
public class CreateModel : PageModel
{
    private readonly RazorPagesContactsContext _context;

    public CreateModel(RazorPagesContactsContext context)
    {
        _context = context;
    }

    public IActionResult OnGet()
    {
        return Page();
    }

    [TempData]
    public string Message { get; set; }

    [BindProperty]
    public Customer Customer { get; set; }

    public async Task<IActionResult> OnPostAsync()
    {
        if (!ModelState.IsValid)
        {
            return Page();
        }

        _context.Customer.Add(Customer);
        await _context.SaveChangesAsync();
        Message = $"Customer {Customer.Name} added";

        return RedirectToPage("./IndexPeek");
    }
}
```

{% endtab %}

{% tab title="Second Tab" %}

<pre class="language-cshtml"><code class="lang-cshtml">@page
@model IndexModel

&#x3C;h1>Peek Contacts&#x3C;/h1>
@{
<strong>    if (TempData.Peek("Message") != null)
</strong>    {
<strong>        &#x3C;h3>Message: @TempData.Peek("Message")&#x3C;/h3>
</strong>    }
}
</code></pre>

<pre class="language-cshtml"><code class="lang-cshtml">@page
@model IndexModel

&#x3C;h1>Contacts Keep&#x3C;/h1>
@{
    if (TempData["Message"] != null)
    {
        &#x3C;h3>Message: @TempData["Message"]&#x3C;/h3>
    }
<strong>    TempData.Keep("Message");
</strong>}
</code></pre>

{% endtab %}
{% endtabs %}
