C# aspx页面重定向到新页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1093081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:03:41  来源:igfitidea点击:

aspx page to redirect to a new page

c#asp.netredirect

提问by Stoob

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 行到最后的所有必要代码,我将不胜感激。

采纳答案by Darin Dimitrov

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

回答by wweicker

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

如果您使用的是 VB,则需要去掉分号:

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

回答by jrummell

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

您也可以在带有元标记的html 中执行此操作:

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

回答by SLaks

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

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

<customErrors mode="off" />

回答by Prasad Jadhav

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:

toRedirect()从客户端调用此函数(例如:body 标签的 onload 事件)或从服务器使用:

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

回答by Mikael Koskinen

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

达林的回答很有效。它创建了一个 302 重定向。这是修改后的代码,以便创建永久的 301 重定向:

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

回答by mad master

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>

回答by marcob

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.

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