aspx page to redirect to a new page

C#asp.netRedirect

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

<%@ Page Language="C#" %>
<script runat="server">
  protected override void OnLoad(EventArgs e)
  {
      Response.Redirect("new.aspx");
  }
</script>

Solution 2 - C#

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>

Solution 3 - C#

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>

Solution 4 - C#

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

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

Solution 5 - C#

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);

Solution 6 - C#

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" />

Solution 7 - C#

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>

Solution 8 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStoobView Question on Stackoverflow
Solution 1 - C#Darin DimitrovView Answer on Stackoverflow
Solution 2 - C#jrummellView Answer on Stackoverflow
Solution 3 - C#Mikael KoskinenView Answer on Stackoverflow
Solution 4 - C#wweickerView Answer on Stackoverflow
Solution 5 - C#Prasad JadhavView Answer on Stackoverflow
Solution 6 - C#SLaksView Answer on Stackoverflow
Solution 7 - C#mad masterView Answer on Stackoverflow
Solution 8 - C#marcobView Answer on Stackoverflow