web analytics

Response.Redirect, Server.Execute and Server.Transfer in ASP.NET

Options

davegate 143 - 921
@2016-02-05 18:14:01

Response.Redirect

The Response.Redirect method causes the browser to redirect the client to a different URL. So, you may run code like:

Response.Redirect("WebForm2.aspx")

or

Response.Redirect("http://www.karlmoore.com/")

to send the user to another page.

@2016-02-05 18:50:33

Server.Transfer

The execution is transfered from one page to another page on the server.The browser client is made one request and the initial page is the one responding with content. Also, any posted form variables and query string parameters are available to the second page as well.

You can't use Server.Transfer to transfer control to a page running on a different server, but you can maintain all the form and querystring variables using the overload version Server.Transfer(<page>, True). The True parameter in that call determines whether the server should preserve the posted values.

@2016-02-05 18:52:10

Server.Execute

This method executes (calls ProcessRequest method on) the HTTP handler corresponding to the given path.  None of the pipeline events (for HTTP Modules or GLOBAL.ASAX) are fired and thus, for example, authentication rules are by-passed.

@2016-02-05 20:43:23

The ThreadAbortException is thrown when you make a call to Response.Redirect(url) because the system aborts processing of the current web page thread after it sends the redirect to the response stream. Response.Redirect(url) actually makes a call to Response.End() internally, and it's Response.End() that calls Thread.Abort() which bubbles up the stack to end the thread.


 

//An instance of ThreadAbortException is thrown

try
{

  Response.Redirect(url);

}
catch (ThreadAbortException ex)
{

//....
}

The line of code that follows Response.Redirect is not executed.
 

There is an overload method of Response.Redirect() without calling Response.End():

Response.Redirect(string url, bool endResponse);

In this overload the second parameter tells the system whether to make the internal call to Response.End() or not. When this parameter is false the client is sent the redirect url, but the internal call to Response.End is skipped. This completely avoids the code that would throw the exception, but the code that follows Response.Redirect is executed and the cost is that this thread doesn't stop executing the Application events!

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com