80

What is the code required to redirect the browser to a new page with an ASPX page?

I have tried this on my page default.aspx :

<% Response.Redirect("new.aspx", true); %>

or

<%@ Response.Redirect("new.aspx", true); %>

And these resulted in a server error that is undetermined. I cannot see the error code; because the server is not in my control and the errors are not public.

Please provide all necessary code from line 1 of the page to the end, and I would really appreciate it.

Roland
  • 127,288
  • 10
  • 191
  • 288
Stoob
  • 2,177
  • 4
  • 19
  • 22

9 Answers9

157
<%@ Page Language="C#" %>
<script runat="server">
  protected override void OnLoad(EventArgs e)
  {
      Response.Redirect("new.aspx");
  }
</script>
talles
  • 14,356
  • 8
  • 45
  • 58
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 11
    What is the purpose of having `base.OnLoad(e);` after `Response.Redirect(..)` ? – Tapan Aug 13 '14 at 14:02
  • Darin I know it's very old answer, but is there a reason for the `base.OnLoad`? – gdoron Feb 01 '16 at 13:27
  • @gdoron I think it's only there because Visual Studio autocomplete inserts it when you start typing the `OnLoad` method. It seems to work just as well without it. – Kevin Panko May 04 '16 at 17:52
  • I wish I wouldn't have up voted this answer. Now I can't down vote it for having 50% redundant code in it... :( Darin you really should edit it or explain why you included the call to the base method. – gdoron May 04 '16 at 18:01
26

You could also do this is plain in html with a meta tag:

<html>
<head>
  <meta http-equiv="refresh" content="0;url=new.aspx" />
</head>
<body>
</body>
</html>
jrummell
  • 42,637
  • 17
  • 112
  • 171
  • 2
    Seriously, what's with the down votes? If all that you need to use is redirect to another page, you don't have to use ASP.Net. If you need to drive a nail and you could choose between a normal hammer and sledge hammer, which would you choose? I hope you'd say the normal hammer. – jrummell Jul 07 '09 at 16:15
  • 2
    This was an html solution to an asp.net problem. – Daniel A. White Jul 07 '09 at 16:29
  • 1
    Right, but html is a HUGE part of ASP.NET. – jrummell Jul 07 '09 at 16:51
  • cut the guy some slack, i gave you a positive just for being nice. this is a good backup way of doing it if the first ASP.NET option hadn't worked. – Stoob Jul 07 '09 at 17:20
  • Thanks for the up vote. I didn't mean to offend anyone. I just wanted to point out that you don't need server side scripting to redirect. – jrummell Jul 07 '09 at 17:30
  • in this case i did need to do it server side, otherwise the HTML would have worked fine – Stoob Jun 11 '10 at 21:20
  • This is a less effective way of redirecting for several reasons. Here's two: 1) it's 301 redirect, so in the future people could still be directed to the old page form search engines 2) there's an extra back-forth for the browser before redirecting to the new page, while the asp.net solution skips that back and forth and goes straight to the new page. – furtive Mar 28 '11 at 21:04
  • While I agree that this is a less-than-ideal solution, there's nothing wrong (in fact it's of benefit) with having multiple choices, and this certainly works. +1 for being a nice guy and offering a working solution. – Ehtyar Aug 11 '11 at 06:05
  • 12
    W3C has deprecated the use of the META element for refreshing or redirecting. http://www.w3.org/TR/WCAG10-HTML-TECHS/#meta-element – Jason Stangroome Jun 27 '12 at 04:15
  • I haven't downvoted but I'd also avoid this solution as it relies on parsing the HTML to follow the redirect - which is fine for most browsers but not for client applications that should all understand how to follow "proper" redirects (Eg try retrieving the contents of the page using the `System.Net.WebRequest` while redirecting using both methods. It'll follow the header, it won't follow the META) – Basic Nov 06 '12 at 13:51
  • 4
    I agree its not the best answer, but still an answer. – jrummell Nov 06 '12 at 14:02
  • @furtive I'd say this was a better way to redirect, as the browser's address bar will reflect the fact that the URL it ended up displaying was different to the one the user requested. Response.Redirect gives the user no clue that the URL has changed. – Jonathan Sayce Nov 13 '13 at 14:22
  • This is not a better way to redirect as it is not a 301 redirect, and so browsers will always point to the wrong (old) URL. – furtive Nov 16 '13 at 18:58
  • the server side code redirect is a 301, or maybe a 302, so in either case it is the clients browser that bounces around. The only difference is the server side issues a true HTTP response header, not an emulated "HTTP-equiv" and the server (depending on how it is set up) would actually issue a 200 OK for the html redirect. So yes, it is better to do the redirect with the correct header which needs be done with the aspx file or have one set up in the webserver (which is usually a better spot to issue redirects, especially when there is no logic conditions) – James Wakefield Feb 02 '15 at 00:39
19

Darin's answer works great. It creates a 302 redirect. Here's the code modified so that it creates a permanent 301 redirect:

<%@ Page Language="C#" %>
<script runat="server">
  protected override void OnLoad(EventArgs e)
  {
      Response.RedirectPermanent("new.aspx");
      base.OnLoad(e);
  }
</script>
Mikael Koskinen
  • 12,306
  • 5
  • 48
  • 63
14

If you are using VB, you need to drop the semicolon:

<% Response.Redirect("new.aspx", true) %>
wweicker
  • 4,833
  • 5
  • 35
  • 60
5

Or you can use javascript to redirect to another page:

<script type="text/javascript">
    function toRedirect() {
        window.location.href="new.aspx";
    }
</script>

Call this toRedirect() function from client (for ex: onload event of body tag) or from server using:

ClientScript.RegisterStartupScript(this.gettype(),"Redirect","toRedirect()",true);
kratenko
  • 7,354
  • 4
  • 36
  • 61
Prasad Jadhav
  • 5,090
  • 16
  • 62
  • 80
2

Even if you don't control the server, you can still see the error messages by adding the following line to the Web.config file in your project (bewlow <system.web>):

<customErrors mode="off" />
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

Redirect aspx :

<iframe>

    <script runat="server">
    private void Page_Load(object sender, System.EventArgs e)
    {
    Response.Status = "301 Moved Permanently";
    Response.AddHeader("Location","http://www.avsapansiyonlar.com/altinkum-tatil-konaklari.aspx");
    }
    </script>

</iframe>
mad master
  • 11
  • 1
0

In a special case within ASP.NET If you want to know if the page is redirected by a specified .aspx page and not another one, just put the information in a session name and take necessary action in the receiving Page_Load Event.

marcob
  • 1
0

Just put the page you want to start with. Someone already put this answer similar. It works and it is easy.

<meta http-equiv="refresh" content="0;url=START_PAGE_Current.html" />