Making a Response.Redirect from plain HTML or JavaScript

Jan 27, 2009 by Constantin


The standard approach for making a URL redirection in ASP.NET is to use a Redirect call:

Response.Redirect("http://www.example.com/mypage.aspx")

What happens behind the scenes: the server (IIS) sends to the browser a simple HTTP Response, containing a message code ("302"), which essentially tells the browser to issue a new HTTP Request, for obtaining the new destination URL. Next, the browser issues that new request, and in the end it receives another page, served from that new URL.

This redirection behavior can be also obtained by plain HTML (no ASP.NET needed), if including in the HTML page a "meta tag":

< html >
   < head >
      < title>index< /title >
      < meta http-equiv="REFRESH" content="0;url=http://www.example.com/mypage.aspx" >
   < /head >
   < body >
      Index page.
   < /body >
< /html >

Or, as an alternative, using a JavaScript code:

< html >
   < head >
      < title >index< /title >
   < /head >
   < body >
      < script language="javascript" >
         window.location = "http://www.example.com/mypage.aspx";
      < /script >
      Index page.
   < /body >
< /html >

Both these approaches, HTML and JS, bring a key advantage: they don't actually involve server-side programming, so you can use it in an ASP.NET application just as well as in a PHP or JSP website. Furthermore, it works just as well in a "static" website (i.e. with no server-side code running in the background).

A more failsafe approach would be to combine the HTML and JS redirection routines, to ensure that the redirection will works on any browser (e.g. if the JavaScript support is disabled in the user's web browser):

< html >
   < head >
      < title >index< /title >
      < meta http-equiv="REFRESH" content="0;url=http://www.example.com/mypage.aspx" >
   < /head >
   < body >
      < script language="javascript" >
         window.location = "http://www.example.com/mypage.aspx";
      < /script >
      Index page.
   < /body >
< /html >

One more note: sometimes Server.Transfer is used instead of Response.Redirect, and the same behavior is inadvertently expected. But Server.Transfer will just transfer the execution to another page's code-behind, so the call stays on the server, it will never send the "302" redirection request to the browser. This means that, from the browser's point of view, the current URL (i.e. the page that is currently viewed) stays the same.

 




Comments


arnold commented on 2/25/2009 6:48:49 AM

Really awesome, i was looking for it....appreciated that.

Thanks;

Constantin Ionescu commented on 9/8/2009 10:14:55 AM

Thanks a lot guys, it sure means a lot! If you have some other problems with ASP.NET that you would like to talk about, feel free to drop a comment about it and I'll try to solve them in future posts.

Pranabesh Saha commented on 9/22/2009 3:05:08 PM

Constantin, Thanks for the nice article. Can you please explain little bit more which one is more efficent:

response.redirect or HTML meta refresh with 0 delay?

Constantin Ionescu commented on 9/24/2009 6:19:28 AM

Thank you for your comment, Pranabesh - actually the Response.Redirect call from ASP.NET just inserts a meta refresh header in the HTML code that is sent to the browser, so both these approaches have the same final result.

So as far as the speed/performance is concerned, these approaches are fully equivalent. In terms of code writing, I guess it's simpler to just write a Response.Redirect, and not bother with the HTML tags syntax.

I hope this helps, let me know if you need any more info on this topic.

Your Comment:







Blog Home   Contact
or go to the SBP website

Recent blog entries



Archives

All tags