簡體   English   中英

aspx頁面重定向到新頁面

[英]aspx page to redirect to a new page

將瀏覽器重定向到帶有ASPX頁面的新頁面需要什么代碼?

我已經在我的頁面default.aspx上嘗試過此操作:

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

要么

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

並且這些導致未確定的服務器錯誤。 我看不到錯誤代碼; 因為服務器不在我的控制范圍內,並且錯誤不公開。

請提供從頁面第1行到末尾所有必要的代碼,我將非常感謝。

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

您也可以使用meta標記在html中實現此功能:

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

達林的答案很棒。 它創建一個302重定向。 這是修改后的代碼,以便創建永久的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) %>

或者,您可以使用javascript重定向到另一個頁面:

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

從客戶端(例如:body標簽的onload事件)或使用以下命令從服務器調用此toRedirect()函數:

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

即使您不控制服務器,也可以通過將以下行添加到項目中的Web.config文件(在<system.web>以下)中來查看錯誤消息:

<customErrors mode="off" />

重定向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