【问题标题】:How to use 301 redirect in asp.net如何在 asp.net 中使用 301 重定向
【发布时间】:2020-01-24 14:18:18
【问题描述】:

我想在我的网站中使用 301 重定向。我尝试将“http://localhost:54996/AboutUs.aspx”重定向到“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);
    }
}

但是这些解决方案都不起作用。当我输入我的地址(http://localhost:54996/AboutUs.aspx)时,我会转到这个地址:“about/”

我的错误在哪里?

【问题讨论】:

    标签: c# asp.net routes http-status-code-301


    【解决方案1】:

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

    我认为您想要的是 url 重写,而不是 重定向

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

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

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

    【讨论】:

      猜你喜欢
      • 2014-07-18
      • 2012-05-27
      • 2011-01-16
      • 2011-06-25
      • 2011-08-24
      • 1970-01-01
      • 2011-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多