Reputation: 179
I am getting runtime exception on use of Response.Redirect()
in global.asax
Results in:
HttpException: Response is not available in this context.
I tried using,
HttpContext.Current.Response.Redirect()
Results in:
NullReferenceExeption: Object reference not set to an instance of an object.
I know that the basic purpose of Global.asax is to handle,
Application level events
Session-level events
But my fundamental question is, Is it a good practice to use Response.Redirect()
in Global.asax
file.. ?
Upvotes: 0
Views: 1967
Reputation: 100547
Yes, you can use Response.Redirect
in Global.asax assuming you are doing in events that have request (like begin request).
You definitely can't use Request during application level events because there is no HttpRequest.Current
available.
Note that while there nothing particularly wrong about having such code in Global.asax, usually such redirect code put into Modules (i.e authentication frequently done as HttpModule and there are plenty redirects), Forms (WebForms) or Controllers (MVC), action attributes (MVC).
Upvotes: 4
Reputation: 88
It's not good or bad practice in particular. What are you actually trying to do? You've provided one line of code without any context whatsoever. Are you trying to redirect certain requests similar to this question, or something else?
The reason you're getting that exception is, not surprisingly, because there is no request context available as the code is running before Begin_Request
.
Upvotes: 2