Microsoft KB Archive/817266

= PRB: System.Threading.ThreadAbortException Error Message When You Use Server.Transfer in HTTPHandler in an ASP.NET Application =

Article ID: 817266

Article Last Modified on 5/5/2003

-

APPLIES TO


 * Microsoft ASP.NET 1.1
 * Microsoft ASP.NET 1.0

-



SYMPTOMS
When you use a Server.Transfer method in HTTPHandler to transfer the control from one page to another in an ASP.NET Web application, you may receive the following error message:

System.Threading.ThreadAbortException: Thread was being aborted. at System.Threading.Thread.AbortInternal at System.Threading.Thread.Abort at System.Threading.Thread.Abort(Object stateInfo) at System.Web.HttpResponse.End at System.Web.HttpServerUtility.Transfer(String path, Boolean preserveForm)



CAUSE
When you call Server.Transfer, ASP.NET internally calls the Server.Execute method to transfer the control, and calls the Response.End method to end the processing of the current page. Response.End ends the page execution and calls the Thread.Abort method. The Thread.Abort method causes the ThreadAbortException error message to appear.



WORKAROUND
To work around this problem, use Server.Execute instead of Server.Transfer in the ProcessRequest method of HTTPHandler. The modified ProcessRequest method is as follows:

Visual C# .NET Sample Code public void ProcessRequest(HttpContext context) {        try {                       context.Server.Execute(&quot;WebForm1.aspx&quot;); }        catch(System.Exception e)         { context.Response.Write(e.StackTrace); context.Response.Write(e.ToString); }     } Visual Basic .NET Sample Code Public Sub ProcessRequest(ByVal context As HttpContext) _ Implements IHttpHandler.ProcessRequest Try context.Server.Execute(&quot;WebForm1.aspx&quot;) Catch e As Exception context.Response.Write(e.StackTrace) End Try End Sub



STATUS
This behavior is by design.



Steps to Reproduce the Behavior
 In Microsoft Visual Studio .NET, use Visual Basic .NET or Visual C# .NET to create a new ASP.NET Web Application project. By default, WebForm1.aspx is created. Name the project ServerTransferTest . Right-click WebForm1.aspx and then click View HTML Source.  Replace the existing code with the following sample code: <%@ Page %> <!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot; >   WebForm1    <asp:HyperLink id=&quot;HyperLink1&quot; style=&quot;Z-INDEX: 101; LEFT: 324px; POSITION: absolute; TOP: 181px&quot; runat=&quot;server&quot; NavigateUrl=&quot;http://localhost/ServerTransferTest/test.ashx&quot;>http://localhost/ServerTransferTest/test.ashx</asp:HyperLink> <asp:Label id=&quot;Label1&quot; style=&quot;Z-INDEX: 102; LEFT: 292px; POSITION: absolute; TOP: 149px&quot; runat=&quot;server&quot;>On Clicking this URL should not throw any exception</asp:Label> </HTML> </li>  Double-click Design View of WebForm1.aspx, and then replace the exiting code in the code behind page with the following sample code:

Visual C# .NET Sample Code using System; using System.Web; using System.Web.UI.WebControls;

namespace ServerTransferTest {   ///     /// Summary description for WebForm1. ///    public class WebForm1 : System.Web.UI.Page {     protected System.Web.UI.WebControls.Label Label1; protected System.Web.UI.WebControls.HyperLink HyperLink1;

private void Page_Load(object sender, System.EventArgs e)       { }

#region Web Form Designer generated code override protected void OnInit(EventArgs e)       { //           // CODEGEN: ASP.NET Web Form Designer requires this call. //           InitializeComponent; base.OnInit(e); }       ///         /// Required method for Designer support - do not modify /// the contents of this method by using the code editor. ///          private void InitializeComponent {          this.Load += new System.EventHandler(this.Page_Load); }       #endregion } }

Visual Basic .NET Sample Code Public Class WebForm1 Inherits System.Web.UI.Page Protected WithEvents Label1 As System.Web.UI.WebControls.Label Protected WithEvents HyperLink1 As System.Web.UI.WebControls.HyperLink


 * 1) Region &quot; Web Form Designer Generated Code &quot;

'Web Form Designer requires this call. <System.Diagnostics.DebuggerStepThrough> Private Sub InitializeComponent

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: Web Form Designer requires this method call. 'Do not modify it by using the code editor. InitializeComponent End Sub


 * 1) End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here End Sub

End Class </li> On the File menu, click Add New Item.</li> Under Add New Item, click Class. In the Name text box under Add New Item, rename the class HelloWorldHandler, and then click Open.</li>  Replace the existing code in the HelloWorldHandler class file with the following sample code:

Visual C# .NET Sample Code using System.Web; namespace ServerTransferTest {  public class HelloWorldHandler : IHttpHandler {     public void ProcessRequest(HttpContext context) {        try {           context.Server.Transfer(&quot;WebForm1.aspx&quot;, true ) ; }        catch(System.Exception e)         { context.Response.Write(e.StackTrace); context.Response.Write(e.ToString); }     }      public bool IsReusable {        // To enable pooling, return true here. // This keeps the handler in memory. get { return false; } }  } }  Visual Basic .NET Sample Code Imports System Imports System.Web

Public Class HelloWorldHandler Implements IHttpHandler Public Sub ProcessRequest(ByVal context As HttpContext) _ Implements IHttpHandler.ProcessRequest Try ' context.Server.Transfer(&quot;WebForm1.aspx&quot;, True) context.Server.Execute(&quot;WebForm1.aspx&quot;) Catch e As Exception context.Response.Write(e.StackTrace) End Try End Sub ' Override the IsReusable property. Public ReadOnly Property IsReusable As Boolean _ Implements IHttpHandler.IsReusable Get Return True End Get End Property End Class </li> Repeat steps 5 and 6 to add another class file. Rename the class file HelloWorldHandlerFactory .</li>  Replace the existing code in the HelloWorldHandlerFactory class file with the following sample code:

Visual C# .NET Sample Code using System.Web; namespace ServerTransferTest {  public class HelloWorldHandlerFactory : IHttpHandlerFactory {     public IHttpHandler GetHandler( HttpContext context, System.String requestType, System.String url, System.String pathTranslated) {        HttpResponse Response = context.Response; Response.Write(&quot; &quot;); Response.Write(&quot; &quot;); Response.Write(&quot; Hello from HelloWorldHandlerFactory.GetHandler &quot;); Response.Write(&quot; &quot;); Response.Write(&quot; &quot;); return new HelloWorldHandler; }     public void ReleaseHandler( IHttpHandler handler ) {     }   } }  Visual Basic .NET Sample Code Imports System Imports System.Web Public Class HelloWorldHandlerFactory Implements IHttpHandlerFactory Public Overridable Function GetHandler(ByVal context As HttpContext, _  ByVal requestType As String, ByVal url As String, ByVal pathTranslated As String) _ As IHttpHandler _ Implements IHttpHandlerFactory.GetHandler

Dim Response As HttpResponse = context.Response Response.Write(&quot; &quot;) Response.Write(&quot; &quot;) Response.Write(&quot; Hello from HelloWorldHandlerFactory.GetHandler &quot;) Response.Write(&quot; &quot;) Response.Write(&quot; &quot;) Return New HelloWorldHandler End Function

Public Overridable Sub ReleaseHandler(ByVal handler As IHttpHandler) _ Implements IHttpHandlerFactory.ReleaseHandler End Sub End Class </li>  Open the Web.config file, and then add the httpHandlers section in the System.Web section as follows: <system.web> <httpHandlers> <add verb=&quot;*&quot; path=&quot;*.ashx&quot; type=&quot;ServerTransferTest.HelloWorldHandlerFactory,ServerTransferTest&quot; /> </httpHandlers> ..... ..... </system.web> </li> On the Debug menu, click Start, and then click the following hyperlink on WebForm1.aspx:

http://localhost/ServerTransferTest/test.ashx

</li></ol>

<div class="references_section">