ASP.NET Core MVC Beginner

State management: all the options

HTTP is stateless: the server forgets everything about a request once it has answered. State management is how your app remembers things between requests, such as a search filter, a message after a redirect, a logged-in user or a shopping cart. This lesson compares every option so you can pick the right one.

Where can an ASP.NET Core app keep state?
  1. Query stringIn the URL, such as ?page=2. Visible, bookmarkable, small
  2. Hidden fieldsInside a form, posted back with it. Users can change them
  3. TempDataSurvives until it is read, perfect for one redirect
  4. CookiesStored by the browser and sent with every request. About 4 KB
  5. SessionData kept on the server, found through a session cookie
  6. HttpContext.ItemsShared between middleware and code for one request only
  7. CacheApp-wide data in server memory or Redis, not per user
  8. DatabasePermanent data that must never be lost

Client-side options travel with requests and can be tampered with. Server-side options stay on the server but use memory or storage.

The idea in plain words

Every request starts fresh: a new HttpContext, a new controller, nothing left over from the last request. If page 2 of a list needs to know the search term from page 1, something has to carry it. You choose where that something lives: in the request itself (URL or form), in the browser (cookies), or on the server (session, cache, database).

Web Forms hid this with ViewState. ASP.NET Core MVC makes it explicit, which keeps pages light and behaviour predictable.

How to choose

Ask how long the data must live and who it belongs to.

  • Needed for one page or a link? Query string.
  • Needed only until the next request after a redirect? TempData.
  • Small user preference, such as a theme? Cookie.
  • Per-user data during a visit, such as a cart for anonymous users? Session.
  • Same data for every user, expensive to load? Cache.
  • Must survive restarts and be shared across servers? Database.

Example: query string and hidden field

The page number travels in the URL and the item id travels in a hidden form field. Model binding reads both.

Razor
@* Link carries state in the query string: /products?page=2&q=shoes *@
<a asp-action="Index" asp-route-page="@(Model.Page + 1)" asp-route-q="@Model.Query">Next page</a>

@* Form carries state in a hidden field *@
<form asp-action="Delete" method="post">
    <input type="hidden" asp-for="Id" />
    <button type="submit">Delete</button>
</form>

Common mistakes

  • Storing large objects in session or cookies, which slows every request.
  • Trusting hidden fields or cookies for prices, roles or anything security related.
  • Using in-memory session or cache on several servers without a distributed store, so users lose data when they hit another server.
  • Using static fields to remember per-user data; static fields are shared by all users.

Interview question

What are the ways to manage state in ASP.NET Core MVC?

Query strings and route values, hidden form fields, cookies, TempData (cookie-based by default, kept until read), session state (server-side data identified by a cookie, needing a cache such as distributed memory or Redis), HttpContext.Items for data within a single request, caching with IMemoryCache or IDistributedCache for shared data, and a database for permanent data. The choice depends on lifetime, size, scope (per user or shared) and security.

Your progress is saved only in this browser.

Want the full details? Read the official Microsoft documentation for this topic.