Making a Response.Redirect from plain HTML or JavaScript

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.

Scroll to Top