【问题标题】:Redirecting Users from Old (Non-Existing) Page to New Page将用户从旧(不存在)页面重定向到新页面
【发布时间】:2011-09-24 15:21:13
【问题描述】:
我在我的 asp.net 4.0 应用程序中删除了旧的登录页面 ClientLogin.aspx,并将其替换为 Login.aspx。每当用户点击旧登录页面时,我希望应用程序自动将用户重定向到新登录页面。我认为在 web.config 中有一种非常简单的方法可以做到这一点。我宁愿不必保留旧页面并使用 Response.Redirect 手动重定向用户。
【问题讨论】:
标签:
asp.net
redirect
url-rewriting
【解决方案2】:
在您的 Global.asax.vb 文件中,在 Application_BeginRequest 事件下添加以下代码:
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
' Fires at the beginning of each request
If Request.Url.AbsoluteUri.ToUpper.Contains("CLIENTLOGIN.ASPX") = True Then
Response.Redirect("Login.aspx")
End If
End Sub
如果您愿意,也可以使用 C#:
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (Request.Url.AbsoluteUri.ToUpper().Contains("CLIENTLOGIN.ASPX") == true)
{
Response.Redirect("Login.aspx");
}
}
【解决方案3】:
在您的情况下,HttpHandler 可能是最好的选择,因为您最终可能希望将其从应用程序中删除。我将使用下面的代码作为起点并对其进行修改以满足您的需求。还要确保您使用永久重定向代码,以便如果这是一个面向公众的网站,Google 或其他搜索引擎会意识到它已经移动。
///
/// Summary description for Redirect.
///
public class Redirect : IHttpHandler
{
public Redirect() {}
#region IHttpHandler Members
public void ProcessRequest(HttpContext context)
{
context.Response.Redirect("Login.aspx");
}
public bool IsReusable
{
get
{
return true;
}
}
#endregion
}
受http://jaysonknight.com/blog/archive/2005/03/31/Using-an-HttpHandler-to-Forward-Requests-to-a-New-Domain.aspx 上的代码启发
编写好 HttpHandler 后,只需在 web.config 中注册它即可开始使用,只需确保将处理程序的路径设置为旧 url。 "ClientLogin.aspx"。
<httpHandlers>
<add verb="*" path="/ClientLogin.aspx" type="My.Web.Redirect, My.Web" />
</httpHandlers>