Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the Pros and Cons of using Global.asax? [closed]

Tags:

asp.net

Let's collect some tips for evaluating the appropriate use of global.asax.

like image 839
Larsenal Avatar asked Sep 25 '08 20:09

Larsenal


People also ask

What is the purpose of using global asax?

Effectively, global. asax allows you to write code that runs in response to "system level" events, such as the application starting, a session ending, an application error occuring, without having to try and shoe-horn that code into each and every page of your site.

Is global asax mandatory?

They defined methods for a single class, application class. They are optional, but a web application has no more than one global. asax file.

Can we run ASP.NET application without global asax file?

asax is not required by ASP.NET for a website to run. It is, however, very useful for application-level functionality (like unhandled exception logging). Save this answer.

How does MVC handle application error in global asax?

Setting HandleError Attribute as a Global Filter Any unhandled exception that takes place within the boundary of the MVC application will now be handled by this global error handler. To test this global handler, comment out the [HandleError] attribute from the action or the controller and then run the application.


2 Answers

It's simple to use if your session and application initialization code is very small and application-specific. Using an HttpModule is more useful if you want to reuse code, such as setting up rules for URL rewriting, redirects or auth. An HttpModule can cover everything a Global.asax file can. They can also be removed and added easily using your .config.

like image 124
Alexander Pendleton Avatar answered Nov 10 '22 14:11

Alexander Pendleton


I've used it before to catch errors at the application level, and to do something when a user's session expires.

I also tend to use it a lot to provide static properties that read values from the web.config.

I think it ok for stuff like that, though I wouldn't put much more than that in there.

like image 45
Sam Schutte Avatar answered Nov 10 '22 14:11

Sam Schutte