Load Session Variables only Once

Kmcnet 1,066 Reputation points
2025-06-10T00:22:18.6833333+00:00

Hello everyone and thanks for the help in advance. This is a continuation of a previous question I asked regarding loading session data. I neglected to fully understand what I needed to accomplish. The data needing to be loaded is used by one user throughout the entire session and only needs to be loaded once. However, the user may enter the application via different pages, so how do I load this data regardless of the page requested. Any help would be appreciated.

ASP.NET Core Training
ASP.NET Core Training
ASP.NET Core: A set of technologies in the .NET Framework for building web applications and XML web services.Training: Instruction to develop new skills.
80 questions
{count} votes

3 answers

Sort by: Oldest
  1. Pradeep M 9,530 Reputation points Microsoft External Staff Volunteer Moderator
    2025-06-10T03:30:48.03+00:00

    Hi Kmcnet

    Thank you for reaching out to Microsoft Q & A forum.     

    To load user specific data once per session and access it from any page, using Session is the best approach. On the first page to request, check if the data exists in session; if not, load it from your Web API and save it to session. Then, all other pages can read the data from the session without reloading. 

    Dependency Injection is great for services but doesn’t persist with user data across requests. You can combine DI with session for cleaner code by injecting a service that handles session access.  

    If you have found the answer provided to be helpful, please click on the "Accept answer/Upvote" button so that it is useful for other members in the Microsoft Q&A community.

    0 comments No comments

  2. SurferOnWww 4,636 Reputation points
    2025-06-10T04:25:22.23+00:00

    This is a continuation of a previous question ...

    You are supposed to show the url of your previous question:

    Persisting Data Through Session

    The data needing to be loaded is used by one user throughout the entire session and only needs to be loaded once.

    Please consider "1. User-Specific Data (Session)" in AgaveJoe's answer given to your previous question.

    0 comments No comments

  3. AgaveJoe 30,121 Reputation points
    2025-06-10T09:35:01.6966667+00:00

    One of the most powerful features of ASP.NET Core is middleware. It allows you, as an ASP.NET developer, to seamlessly integrate your custom logic directly into the HTTP pipeline. This means you can execute specific code for every incoming request and outgoing response.

    For your particular need, checking if a session exists is straightforward. You would simply add an if statement within your middleware to perform that check.

    http://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-9.0


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.