Persisting Data Through Session

Kmcnet 1,066 Reputation points
2025-05-29T17:51:07.91+00:00

Hello everyone and thanks for the help in advance. I am developing an ASP.Net Core website that, upon the first page load, calls a WebApi to retrieve that will be used throughout the entire application and persisted from page to page. I have a hazy idea on how to do this with session variables, but not sure if this is the best method and would prefer to utilize dependency injection, but again, not exactly sure the best way to implement. Any help would be appreciated.

Community Center Not monitored
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 30,126 Reputation points
    2025-05-29T19:34:20.2533333+00:00

    Deciding how and where to store data depends on how long you need it to "stick around." Here's a breakdown of common persistence strategies:


    1. User-Specific Data (Session)

    If you need to store data unique to a single user for the duration of their visit (like items in a shopping cart), Session is your go-to. It's designed for "per-user" data. For small amounts of data, you can even configure Session to use cookies, avoiding server-side storage.


    1. Application-Wide Data (Singleton Service)

    When data needs to be available across your entire application for its lifetime (e.g., configuration settings or frequently accessed static data), consider creating a service with a Singleton lifetime. This means there's only one instance of the service, shared by all requests, for as long as your application is running.


    1. Reducing Database Calls (Caching)

    If your primary goal is to improve performance by reducing the number of times you hit your database for the same data, caching is an excellent strategy. You can store frequently retrieved data in memory or a distributed cache, serving it much faster than re-querying the database.


    Hopefully, this clarifies the different levels of data persistence and helps you choose the right approach! Do any of these scenarios sound like what you're trying to achieve?

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.