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.
If you are using VB, you need to drop the semicolon:
<% Response.Redirect("new.aspx", true) %>
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.
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>
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);
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>
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" />
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>
Source: Stackoverflow.com