如何在 asp.net 中使用 301 重定向

How to use 301 redirect in asp.net

我想在我的网站上使用 301 重定向。我尝试重定向“http://localhost:54996/AboutUs.aspx" to "http://localhost:54996/About”。 我在页面加载中使用此代码:

Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", "http://localhost:54996/About");

或此代码:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    if (Request.FilePath == "/AboutUs.aspx")
    {
         Response.RedirectPermanent("/About", true);
    }
}

但是 none 这些解决方案有效。当我输入我的地址(http://localhost:54996/AboutUs.aspx)时,我会转到这个地址:"about/"

我的错误在哪里?

重定向 301 或 302 是正确的响应代码,网络爬虫使用它来判断页面是暂时还是永久移动了位置。

我想你想要的是url重写,而不是重定向

一个很好的方法是在 IIS 中进行所有重定向,在它到达 Application_BeginRequest 事件之前在管道的早期进行: https://www.iis.net/downloads/microsoft/url-rewrite

使用代码指定的 URL 必须是绝对值 URL 否则它将尝试重定向到您的应用程序中。

Response.RedirectPermanent(<URL> + "/About..