so why ApplicationStates.UserDto display null on second page (dashboard) although it initialized correctly on first page(login)
Because you on startup.cs add on configure service :
services.AddScoped<ApplicationStates>();
Scoped — Services are created on each request (once per request). So for example, if during a request you use the same dependency injection, in many places, you will use the same instance of that object, it will make reference to the same memory allocation. But when you go to the second page (dashboard), it's the another request, so the Services are created again, and you don't give the value to ApplicationStates.UserDto , that's why ApplicationStates.UserDto it display null on dashboard Page(second page).
How to solve this issue Expected result second page ApplicationStates.UserDto must have data
Singleton — Services are created once for the lifetime of the application. It uses the same instance for the whole application. So try to on startup.cs add on configure service:
services.AddSingleton<ApplicationStates>();
result:
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Qing Guo