Using ASP.NET for global.asax

When to use and not use global.asax file in asp.net application? I heard that you should use this file only as a last resort.

+6
source share
3 answers

The Global.asax file is used to implement application events and session level, such as:

Application_Init - Runs when the application first initializes.

Application_Start - starts when the application is first launched

Application_End - The final event that was fired when the application expired or expired.

Session_Start - Runs on the first start of a user session.

Application_BeginRequest - runs with each new request

Application_EndRequest - launched when the application terminates

Application_AuthenticateRequest - an event indicates that the request is ready for authentication.

Application_Error - fires when an unhandled error occurs in the application

Session_End - triggered whenever a single user session ends or time runs out.

Implementing these handlers may be a legitimate use of global.asax. For example, the Application_Error event handler typically logs any global errors, and the Application_End event handler typically contains application cleanup logic. This is a good use of Global.asax. Use them when necessary, and don't be afraid if the file grows.

However, I saw cases where developers added all the global methods to global.asax, which are really not justified. For example, drive business logic associated with a specific domain object within the object itself, and not in global.asax. If you find methods in Global.asax that shouldn't have refactoring, work in the right place.

+6
source share

global.asax is an HTTPModule. All requests go through global.asax and other modules before they reach your page handlers. Use this to accomplish specific tasks for your request or response, such as URL routing, global error, etc.

+1
source share

If you need something special at the beginning / end of the application or at the beginning / end of a session or globally handle exceptions, you can use it to map events in Apllication and Session loops.

0
source share

All Articles