I understand they both don't change the URL that the client sees. Is there anything in them that makes one of them preferable over the other?
I'm planning to use it in the Application_BeginRequest in Global.asax, but also in regular aspx page.
So, in brief: Response. Redirect simply tells the browser to visit another page. Server. Transfer helps reduce server requests, keeps the URL the same and, with a little bug-bashing, allows you to transfer the query string and form variables.
Redirect() and Server. Transfer(). Transfers the page control to the other page, in other words it sends the request to the other page. Causes the client to navigate to the page you are redirecting to.
In a browser with developer tools enabled, make a request to the sample app with the path /redirect-rule/1234/5678 . The regular expression matches the request path on redirect-rule/(. *) , and the path is replaced with /redirected/1234/5678 . The redirect URL is sent back to the client with a 302 - Found status code.
I think Context.RewritePath()
is the better option.
Reason:
Server.Transfer()
throws a ThreadAbortException
every time. The result of calling Response.End()
.
For more details read the following MS articles:
More Information:Server.Transfer()
does not send a HTTP 302 redirect command as Response.Redirect()
would do.
According to HttpContext.RewritePath on MSDN, RewritePath()
is used in cookieless session state.
Also, on a different subject, Server.Transfer()
and Server.Execute()
are very different:
Server.Execute()
returns control to the initial page immediately after where it was called.
For Example:
<div>
test 1 <br/>
<% Server.Execute("include.aspx?hello=ok"); %>
test 2 <br/>
</div>
Would output:
test 1
content of include.aspx?hello=ok
test 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With