Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server.Transfer vs. Context.RewritePath

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.

like image 803
Lea Cohen Avatar asked Dec 03 '08 10:12

Lea Cohen


People also ask

What is the difference between Server transfer and Response redirect?

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.

Which method is used to redirect user from directly from Server?

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.

How to rewrite URL in ASP net Core?

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.


1 Answers

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:

  • ThreadAbortException Occurs If You Use Response.End, Response.Redirect, or Server.Transfer
  • HttpServerUtility.Transfer Method on MSDN

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

like image 61
netadictos Avatar answered Oct 22 '22 20:10

netadictos