D
D
Dmitry Gavrilenko2016-10-27 20:47:11
ASP.NET
Dmitry Gavrilenko, 2016-10-27 20:47:11

ASP NET How to store data inside an application?

Hi all.
I am learning ASP.NET Core.
Now I'm learning MVC. I would like to ask you how to store data common to all Actions?
For example: a regular Home controller, with methods (Actions) Index and Index2. The controller has a property In both View I pass this property. I go through the pages, but the property is not incremented. From this we can assume that for each request a new instance of the controller is created. You can make this property static, then everything will be fine. But as for me, this method is not the best. I also don't like storing data in sessions or cookies. Even more so DB. Where is data usually written for "global" storage? UPD:
public int Counter { get { return _c++; } }
I clarify about the operation of the controller. The constructor fires on every call, but the destructor never fires. Therefore, the question is brewing, but will this controller fill up all the memory?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Nemiro, 2016-10-27
@AlekseyNemiro

The application has start and end events.
Shared data can be loaded at application startup ( Application_Start ) or loaded into static classes while the application is running. Or use HttpApplication , although there is not much point in this.
Important information can be saved at the end of the application ( Application_End ) to any convenient storage (databases, files). If the data is very important, then it is better to save them periodically while the application is running (make a timer when the application is initialized), because the workflow may not terminate correctly and the Application_End eventmay not occur. You should also consider the possibility of an unexpected termination of the application while saving important data (for example, if the worker process is killed).
Temporary data can be stored in a cache.
Small amounts of data can be stored in variables, but it is better to try to keep such data in one place and not spread it all over the application. For example, using controller classes for this purpose is not a good idea.

S
Sergey, 2016-10-27
@senal

ASP.NET usually state less - does not store states. The application can be stopped/started at any moment (for example by IIS) and all static variables will lose their values. If you need storage (persistence), then choose: files, databases, external caches, etc.
It is more correct to store counters in an external cache (for example, in redis).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question