RedirectToRoute actually wraps Response.Redirect minus false to complete the request - hence the request continues. You can use HttpApplication.CompleteRequest as an immediate call to complete the request so that the following application events are not triggered.
Response.End (and another redirect change) throws a ThreadAbortException to interrupt the request processing thread, which is really a bad way to stop request processing. In the .NET world, exception handling is always considered expensive because the CLR then needs to search the stack in all directions for exception handling blocks, create a stack trace, etc. IMO, CompleteRequest was introduced in .NET 1.1 to avoid that it actually relies on the installation flag in the ASP.NET infrastructure code to skip further processing except for the EndRequest event.
Another (and best) way is to use Server.Transfer and avoid returning the client to both ends to set up redirection at the same time. The only problem is that the client will not see the redirected URL in the address bar of the browser. I usually prefer this method.
EDIT
CompleteRequest will never work in the case when subsequent events of the page will be called, because the page is a handler, all its events occur in one (and the current) ProcessRequest application. So the only way is to set the flag and check this flag in overrides like Render , PreRender , RaisePostBackEvent , etc.
From a service point of view, it makes sense to have this functionality in the base page class (i.e., support the flag by proposing the CompleteRequest method for subclasses and overriding lifecycle event methods). For example,
internal class PageBase: System.Web.UI.Page { bool _requestCompleted; protected void CompleteRequest() { Context.ApplicationInstance.CompleteRequest(); _requestCompleted = true; } protected override void RaisePostBackEvent(IPostBackEventHandler sourceControl, string eventArgument) { if (_requestCompleted) return; base.RaisePostBackEvent(sourceControl, eventArgument); } protected internal override void Render(HtmlTextWriter writer) { if (_requestCompleted) return; base.Render(writer); } protected internal override void OnPreRender(EventArgs e) { if (_requestCompleted) return; base.OnPreRender(e); } ... and so on }
VinayC
source share