ASP.NET - global location for code execution?

I have a question with a newbie .net.

Where would be a good place to write code that runs globally before loading each page? Some of the operations that I will do are as follows:

a) open a database connection b) check the session to see if the user is logged in c) it is possible to initialize several objects

I thought about this in the main file, but was not sure if this is a good idea. And, perhaps, for each of these operations, there may be a function in .Net that does this already, so I don’t need to “reinvent” the wheel.

+4
source share
3 answers

I would create a base page where all pages are inherited (except the main pages, since they have a different base class)

You can then override PreLoad on the base page and perform your checks.

+4
source

You can create a base class derived from Page , and has a Page_Load implementation that performs the necessary operations. Then you must change all of your pages to inherit this new base class.

+2
source

This will be the Global.asax file .

0
source

Source: https://habr.com/ru/post/1347580/


All Articles