【问题标题】:301 Redirect in Asp.Net MVCAsp.Net MVC 中的 301 重定向
【发布时间】:2011-05-27 13:50:38
【问题描述】:

我有一个多元文化 MVC2 网站。 其实我的主页可以通过以下路径访问:

http://mydomain.com
http://mydomain.com/
http://mydomain.com/en
http://mydomain.com/en/
http://mydomain.com/en/home
http://mydomain.com/en/home/

我想要的是以上所有路径都将 301 重定向到以下路径:

http://mydomain.com/en

这样我就不必在不同的 url 之间共享 pagerank。

请注意,en 字符串是动态的,用于设置网站的文化。

我是 Asp.Net MVC 的新手,有人可以发布一些代码来做到这一点吗? 谢谢

【问题讨论】:

    标签: asp.net asp.net-mvc-2 http-status-code-301


    【解决方案1】:

    您可以创建自定义操作结果。看到这个帖子:http://forums.asp.net/p/1337938/2700733.aspx

    【讨论】:

      【解决方案2】:

      类似的东西

      public class PermanentRedirectResult : ViewResult
      {
          public string Url { get; set; }
      
          public PermanentRedirectResult(string url)
          {
              if (string.IsNullOrEmpty(url))
                  throw new ArgumentException("url is null or empty", url);
              this.Url = url;
          }
      
          public override void ExecuteResult(ControllerContext context)
          {
            if (context == null)
              throw new ArgumentNullException("context");
            context.HttpContext.Response.StatusCode = 301;
            context.HttpContext.Response.RedirectLocation = Url;
            context.HttpContext.Response.End();
          }
      }
      

      并用这个来调用它

      返回新的 PermanentRedirectResult("/myurl");

      【讨论】:

      • 投反对票?为什么在这个答案发布 2 年后投反对票,至少你可以做的是提供评论?还要记住这是 MVC 版本 2!
      猜你喜欢
      • 2011-06-25
      • 2013-06-03
      • 2011-05-23
      • 2016-05-11
      • 2011-01-11
      • 2011-01-16
      • 1970-01-01
      • 2017-10-08
      • 2011-08-24
      相关资源
      最近更新 更多