简体   繁体   English

aspx页面重定向到新页面

[英]aspx page to redirect to a new page

What is the code required to redirect the browser to a new page with an ASPX page? 将浏览器重定向到带有ASPX页面的新页面需要什么代码?

I have tried this on my page default.aspx : 我已经在我的页面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. 请提供从页面第1行到末尾所有必要的代码,我将非常感谢。

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

You could also do this is plain in html with a meta tag : 您也可以使用meta标记在html中实现此功能:

<html>
<head>
  <meta http-equiv="refresh" content="0;url=new.aspx" />
</head>
<body>
</body>
</html>

Darin's answer works great. 达林的答案很棒。 It creates a 302 redirect. 它创建一个302重定向。 Here's the code modified so that it creates a permanent 301 redirect: 这是修改后的代码,以便创建永久的301重定向:

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

如果使用的是VB,则需要删除分号:

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

Or you can use javascript to redirect to another page: 或者,您可以使用javascript重定向到另一个页面:

<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: 从客户端(例如:body标签的onload事件)或使用以下命令从服务器调用此toRedirect()函数:

ClientScript.RegisterStartupScript(this.gettype(),"Redirect","toRedirect()",true);

即使您不控制服务器,也可以通过将以下行添加到项目中的Web.config文件(在<system.web>以下)中来查看错误消息:

<customErrors mode="off" />

Redirect aspx : 重定向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>

在ASP.NET中的一种特殊情况下,如果您想知道该页面是否由指定的.aspx页面而不是另一个页面重定向,只需将信息放在会话名称中,并在接收的Page_Load事件中采取必要的措施。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM