c# - 显示请求页面的404页面

标签 c# asp.net vb.net asp-classic umbraco

我最近将网站迁移到了新的CMS(Umbraco)。许多链接已更改,但是可以通过在URL中搜索模式来轻松纠正它们,因此,我想写一些东西,如果找不到旧的页面,将重定向到正确的页面。那部分不是问题。

将浏览器重定向到我的自定义404页面后,如何获取请求的URL。我尝试使用:

request.ServerVariables("HTTP_REFERER") 'sorry i corrected the typo from system to server.


但这没有用。

有任何想法吗?

该站点位于IIS 6.0上。
我们确实考虑过使用301重定向,但是我们无法知道人们为哪些页面添加了书签并且有几百个页面,因此没有人愿意花时间创建301重定向。

最佳答案

我做的基本上与您在自定义404错误处理页面中要求的相同。在IIS 6上,原始URL在查询字符串中。下面的代码显示了如何获取原始URL,然后转发用户。以我为例,我从旧的ASP切换到新的ASP.NET,因此所有的.asp页都必须转发到.aspx页。另外,某些网址已更改,因此我在旧网址中查找关键字并转发。

//did the error go to a .ASP page?  If so, append x (for .aspx) and 
//issue a 301 permanently moved
//when we get an error, the querystring will be "404;<complete original URL>"
string targetPage = Request.RawUrl.Substring(Request.FilePath.Length);

if((null == targetPage) || (targetPage.Length == 0))
    targetPage = "[home page]";
else
{
     //find the original URL
    if(targetPage[0] == '?')
    {
        if(-1 != targetPage.IndexOf("?aspxerrorpath="))
             targetPage = targetPage.Substring(15); // ?aspxerrorpath=
        else
             targetPage = targetPage.Substring(5); // ?404;
        }
        else
        {
             if(-1 != targetPage.IndexOf("errorpath="))
             targetPage = targetPage.Substring(14); // aspxerrorpath=
             else
            targetPage = targetPage.Substring(4); // 404;
        }
    }               

    string upperTarget = targetPage.ToUpper();
    if((-1 == upperTarget.IndexOf(".ASPX")) && (-1 != upperTarget.IndexOf(".ASP")))
    {
        //this is a request for an .ASP page - permanently redirect to .aspx
        targetPage = upperTarget.Replace(".ASP", ".ASPX");
        //issue 301 redirect
        Response.Status = "301 Moved Permanently"; 
        Response.AddHeader("Location",targetPage);
        Response.End();
    }

    if(-1 != upperTarget.IndexOf("ORDER"))
    {
                //going to old order page -- forward to new page
               Response.Redirect(WebRoot + "/order.aspx");
           Response.End();
    }

关于c# - 显示请求页面的404页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/150329/

相关文章:

c# - Visual Studio 模板 - 添加其他预先存在的项目

c# - 在 RowDeleteing 事件中显示 ASPX Popup 控件 (ASPX Gridview)

c# - 在构造函数中手动添加 Application Start 事件处理程序

asp.net - 从代码隐藏更新 CSS 属性

asp.net - 使用 Azure Active Directory 进行单点登录

mysql - 在VB.Net中的标签上显示数据库记录

c# - WPF 的 ActiveX VLC 插件不会静音

c# - Visual Studio 2010 错误 : "Exceptions has been thrown by the target of an invocation"

vb.net - 为什么 FileSystemWatcher 触发两次

javascript - page.aspx和code behind(page.aspx.vb)关系问题