繁体   English   中英

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

[英]How to use 301 redirect in asp.net

我想在我的网站中使用 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 )时,我将 go 输入到这个地址:“about/”

我的错误在哪里?

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM