Let's collect some tips for evaluating the appropriate use of global.asax.
-
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.
From Sam Schutte -
Global.asax can inherit from your own class that inherits httpapplication. Gives you more options as well as putting the bulk of the code you might have in global into a class library.
EDIT: Having your HttpApplication class (global.asax parent) in a seperate class library can promote reusability too. Although i agree that using HttpModules is better suited for many tasks, but this still has many uses, for one, cleaner code.
From mattlant -
I haven't really used Global.asax. I used it's equivalent in classic ASP all the time, but that had mostly to do with certain configurations like database connection strings and such. The config in .net makes a lot of these things a lot easier.
But if you want to implement application and session level events, this is where ou need to go.
From Charles Graham -
It's a nice spot to grab session initiation, and even request initiation. Others have mentioned the error handing aspect, although be careful of exceptions thrown from non-asp.net threads (eg. threadpool or custom thread) as they'll bypass the global.asax handler. Personally I always have one, I think of it as simply part of the plumbing.
From WaldenL -
- Initializing ASP.NET MVC. :)
- Custom user authentication.
- Dependency injection, like extending the Ninject HttpApplication.
From hangy -
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.
From Alex -
I used to use Global.asax for things such as error handling etc, however, I have since gone to using HttpModules to replace this as I can copy it from one project to another without editing the global.asax.
From Mitchel Sellers -
Its a stiff drink, just be careful not to drink too much and you'll be ok.
I use it for global error handling, and setting up routes in mvc. You don't want to be writing global page_init code in there though.
If you stick to application level events, and make most of the logic actually live in classes that just get called during those events, you will have no problem making use of the global constructs.
From DevelopingChris -
Con of using Global.asax compared to an HttpModule : You will be tempted to write code that's hard to reuse because it will be too tied to that particular application.
From Andrei Rinea -
The
Session_Start
event in Global.asax is a wicked good place to initialize your session variables.From Slim
0 comments:
Post a Comment