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.

Smithd581 commented on 5/18/2014 4:14:03 AM

I discovered your blog web site website on the search engines and check several of your early posts. Always sustain up the very excellent operate. I lately additional increase Rss to my MSN News Reader. Looking for toward reading much far more on your part later on! kdkdbddkddcddakf

Pharmd315 commented on 5/18/2014 5:21:18 PM

Very nice site! <a href="http://yieapxo2.com/qoqsro/1.html">cheap goods</a>

Pharmd754 commented on 5/18/2014 5:22:44 PM

Very nice site! [url=http://yieapxo2.com/qoqsro/2.html]cheap goods[/url]

Pharmd523 commented on 5/18/2014 5:23:44 PM

Very nice site! cheap goods http://yieapxo2.com/qoqsro/4.html

Pharmd7 commented on 5/18/2014 5:24:41 PM

Very nice site!

Pharmc371 commented on 5/20/2014 12:38:21 AM

Very nice site! [url=http://ypxaieo2.com/ooovqoy/2.html]cheap goods[/url]

Pharmg218 commented on 5/20/2014 12:39:25 AM

Very nice site! cheap goods http://ypxaieo2.com/ooovqoy/4.html

Pharme308 commented on 5/20/2014 12:39:56 AM

Very nice site!

Pharmf417 commented on 5/21/2014 7:50:05 AM

Very nice site! <a href="http://oixypea2.com/qxqsrqq/1.html">cheap goods</a>

Pharmd984 commented on 5/21/2014 7:50:22 AM

Very nice site! [url=http://oixypea2.com/qxqsrqq/2.html]cheap goods[/url]

Pharmd544 commented on 5/21/2014 7:50:52 AM

Very nice site! cheap goods http://oixypea2.com/qxqsrqq/4.html

Pharmc837 commented on 5/21/2014 7:51:15 AM

Very nice site!

Pharmd944 commented on 5/22/2014 2:10:57 PM

Very nice site! <a href="http://opxyiea2.com/yoyaoyo/1.html">cheap goods</a>

Pharme645 commented on 5/22/2014 2:11:34 PM

Very nice site! [url=http://opxyiea2.com/yoyaoyo/2.html]cheap goods[/url]

Pharmc94 commented on 5/22/2014 2:12:27 PM

Very nice site! cheap goods http://opxyiea2.com/yoyaoyo/4.html

Pharmd751 commented on 5/22/2014 2:12:57 PM

Very nice site!

Pharmf800 commented on 5/23/2014 8:43:27 PM

Very nice site! <a href="http://oixypea2.com/oxovqor/1.html">cheap goods</a>

Pharmf9 commented on 5/23/2014 8:43:47 PM

Very nice site! [url=http://oixypea2.com/oxovqor/2.html]cheap goods[/url]

Pharmd989 commented on 5/23/2014 8:44:10 PM

Very nice site! cheap goods http://oixypea2.com/oxovqor/4.html

Pharmd301 commented on 5/23/2014 8:44:41 PM

Very nice site!

Pharmg55 commented on 5/25/2014 4:29:29 AM

Very nice site! <a href="http://yieopxa2.com/yxyaoyx/1.html">cheap goods</a>

Pharmd853 commented on 5/25/2014 4:30:18 AM

Very nice site! [url=http://yieopxa2.com/yxyaoyx/2.html]cheap goods[/url]

Pharmg579 commented on 5/25/2014 4:30:50 AM

Very nice site! cheap goods http://yieopxa2.com/yxyaoyx/4.html

Pharmk180 commented on 5/25/2014 4:31:16 AM

Very nice site!

Pharmf28 commented on 5/25/2014 10:38:13 AM

Very nice site! <a href="http://yieopxa2.com/yxyaoyx/1.html">cheap goods</a>

Pharmb46 commented on 5/25/2014 10:38:38 AM

Very nice site! [url=http://yieopxa2.com/yxyaoyx/2.html]cheap goods[/url]

Pharmg191 commented on 5/25/2014 10:39:07 AM

Very nice site! cheap goods http://yieopxa2.com/yxyaoyx/4.html

Pharmb483 commented on 5/25/2014 10:39:28 AM

Very nice site!

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

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

Your Comment:







Blog Home   Contact
or go to the SBP website

Recent blog entries



Archives

All tags