Lea Cohen Lea Cohen - 1 year ago 276
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 Source

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:

    test 1 <br/>
    <% Server.Execute("include.aspx?hello=ok"); %>
    test 2 <br/>

Would output:

test 1
content of include.aspx?hello=ok
test 2

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download