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


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.
     

Roark commented on 6/1/2014 9:26:21 PM

If you copy the code from here remove the spaces in the tags.
     

Game of War - Fire Age Cheats commented on 8/9/2015 11:38:01 AM

So the remedy for this problem is to modify the "last modified" date-time of your D L L, and create it small than the server's time. You can do this using the Contact .exe device, from Windows 2000 Source Kit.
     

buying followers for instagram commented on 8/22/2015 10:55:03 AM

Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained!
     

EYP MISSION CRITICAL commented on 8/30/2015 12:43:43 PM

I have a phenomenal wife and daughter, and when I am not programming or fixing computers I get pleasure from spending time in the out doors. There are other fun issues there too, akin to top quickest issues on the web which you could vote on. If your trying to make a computer run faster this is your #1 useful resource! Hardware is any tangible merchandise in a pc system, like the system unit, keyboard, or printer. Software program, or a pc program, is the set of instruction that direct the computer to carry out a activity.
     

Your Comment:







Blog Home   SBP Home
RSS Feed       Contact








 Blog Archives  |  Terms of Use  |  Privacy Policy
Copyright © 2015 SBP Romania. All rights reserved.