Lea Cohen Lea Cohen - 3 months ago 46
ASP.NET (C#) Question

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.

Answer

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