【问题标题】:ASP.NET Response.Redirect uses 302 instead of 301ASP.NET Response.Redirect 使用 302 而不是 301
【发布时间】:2011-11-20 15:40:26
【问题描述】:

使用以下代码

context.Response.StatusCode = 301;

context.Response.Redirect(newUrl, true);
context.Response.End();

我可以在 fiddler 中看到它使用的是 302 而不是 301。我应该在重定向调用之后设置状态吗?

【问题讨论】:

  • 我知道这是一个很老的问题,但是为了后代的利益,应该注意的是,重定向代码 301 通常会适得其反;如果您必须更改此重定向,包括将其还原并将内容托管在您重定向的原始位置,则该站点将为已重定向的任何用户中断,并且清除缓存的 301 条目可能需要“高级用户”知识以及普通网站访问者没有的访问权限。没有缓存 302,虽然这会减慢加载速度,但可以随意更改,浏览器会正确响应。

标签: asp.net redirect


【解决方案1】:

如果您使用的是 ASP.Net 4.0,则可以使用 Response.RedirectPermanent,它将使用 301 而不是 302。

【讨论】:

    【解决方案2】:

    Response.Redirect() 将使用重定向代码 (302) 覆盖 StatusCode 属性。此外,由于您使用的是带有布尔参数的 Response.Redirect() 重载,因此如果您想自己调用 Response.End(),则应将其设置为 False。否则它是多余的并且可能导致错误。

    尝试以下方法(ASP.NET 4.0 之前;Adam Butler 的回答涵盖了新的最佳实践):

    context.Response.Redirect(newUrl, false);
    context.Response.StatusCode = 301;
    context.Response.End();
    

    【讨论】:

    • +1 这也对我有用!非常感谢这个小提示。
    【解决方案3】:

    301 是可缓存的。如果你使用的是 ASP.NET 4.0,你可以使用RedirectPermanent

    另外,在重定向之后设置你的状态码

    另外,请查看这些答案。 Response.Redirect HTTP status code

    【讨论】:

      【解决方案4】:

      如果我想要重定向到当前网站的不同版本的旧域/子域,我会将上述答案与我使用的东西结合起来,主要是出于 SEO 原因,以免有多个版本的不同网址的同一个网站:

      using System;
      using System.Net;
      using System.Web;
      using System.Web.Http;
      using System.Web.Mvc;
      using System.Web.Optimization;
      using System.Web.Routing;
      
      namespace myapp.web {
        public class Global : HttpApplication {
          void Application_Start(object sender, EventArgs e) {
            // Code that runs on application startup
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
          }
      
          protected void Application_BeginRequest(object sender, EventArgs e) {
            //some of these checks may be overkill
            if ((HttpContext.Current != null)
              && (HttpContext.Current.Request != null)
              && (HttpContext.Current.Request.ServerVariables != null)
              && (!String.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["HTTP_HOST"]))
              ) {
              switch (HttpContext.Current.Request.ServerVariables["HTTP_HOST"]) {
                case "old.url.com":
                  HttpContext.Current.Response.RedirectPermanent("https://new.url.com", true);
                  //status code is not needed if redirect perm is used
                  HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.MovedPermanently;
                  HttpContext.Current.Response.End();
                  break;
                case "nightly.old.url.com":
                  HttpContext.Current.Response.RedirectPermanent("https://nightly.new.url.com", true);
                  //status code is not needed if redirect perm is used
                  HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.MovedPermanently;
                  HttpContext.Current.Response.End();
                  break;
              }
            }
          }
        }
      }
      

      【讨论】:

        【解决方案5】:

        对我有用

        Response.Status="301 Moved Permanently"
        Response.AddHeader "Location", "NewURL"
        Response.end
        

        【讨论】:

          猜你喜欢
          • 2017-01-28
          • 2019-04-12
          • 2010-09-07
          • 2015-03-15
          • 2012-02-04
          • 2021-10-19
          • 1970-01-01
          • 2023-03-07
          相关资源
          最近更新 更多