Thomas’ Developer Blog

March 14, 2008

response.redirect vs server.transfer and PostBackURL

Almost everyone knows about response.redirect.  Response.redirect simply sends a request to the client asking for the page to be redirected to the requested URL.  This is generally a very easy to use method, but there are a lot of downfalls.

First being you loose all form data on the redirect, so you have to sit there and figure out what was lost, which normally isn’t the case.  Second you’re sending yet another request to the server.  More lag, less performance.

Server.Transfer is an excellent choice for certain pages!  Like submission pages that may require multiple pages or resources.  You can transfer the code over to another file without having the client even know they are looking at another page!  The url doesn’t change or anything.  Beautiful choice!  I myself am planing on using this method on a search page, with the help of querystrings to store bookmarking information.

The downside of Server.Transfer is the fact that you do loose the ability to bookmark in most cases!  Again this can be overcome by using a querystring value.  Also debugging can be a bit harder, but again not a huge deal since you can typically undrstand where the problem is ahead of time. 

As far as the last, PostBackURL.  One of my biggest problems with asp.net is that pesky postback!  Always wants to show the page name.  The reason is .Net likes to do everything from the server, which it should, but as a result it shows the page name.

My urls are typically like… http://www.mysite/home/ which leaves the page name hidden.  Extra bit of security!  Well when asp.net sends a postback it comes out like… http://www.mysite/home/index.aspx which then tells the useres, hey this guy uses aspx!  I know some aspx!  Let’s go ahead and try to hack it.  Ok it sounds funny, but you forget most of your hackers are teenagers with nothing better to do.  So always hide as much as possible!

PostBackURL lets you do this!  I’ll include some VB code to help you out with getting to the same page without manually typing it in:

http://” & Request.ServerVariables(“SERVER_NAME”) & Left(Request.ServerVariables(“URL”),InStrRev(Request.ServerVariables(“URL”),”/”))

Really simple code that returns the same page your on without showing the page name.  Just throw that in with your PostBackURL property and you’re good to go.  Anyway I hope that helps you all out.

Advertisements

9 Comments »

  1. Thomas,

    Server.Transfer also hide “index.aspx” doesn´t it?

    Could not get the diff between Server.Transfer and PostBackUrl.

    Regards

    Comment by Fergara — September 16, 2008 @ 4:25 pm

  2. Server transfer will hide index.aspx only because server.transfer only redirects the server to another page, but leaves the client’s URL the same.

    So if someone is visiting

    site.com/forums/submit

    and then submits that forum entry, you can user server.transfer to redirect them to site.com/forums/confirm, while allowing that users to still think they are on the submit page.

    PostBackURL on the other hand is used for form objects, so that on post back it will submit that form information to another page.

    This only works on later versions of IIS for security reasons. I prefer postbackurl as the best way to hide the index.aspx, but if you are using IIS 5 or below you’ll need to look into URL rewriting to perform this task.

    Hope this helps

    Comment by sanzon — September 17, 2008 @ 9:46 pm

  3. Really very good contents

    Comment by Tarun — November 7, 2008 @ 11:00 am

  4. In this post you comment on Response.Redirect as if it is the worst of these three options, but keep in mind that what you suggest as downsides can be absolutely necessary in certain cases. Such as, if you’re redirecting to the same page with different page parameters. If the form data remains, it could corrupt the new values that should be taking their place.

    Just thought that those attributes should have their good points raised as well.

    Comment by sinkaidas — September 4, 2009 @ 10:28 pm

  5. Can you tell us more about this? I’d love to find out some additional information.

    Comment by first trimester — October 4, 2014 @ 4:04 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: