ASP.NET MVC 中的301重定向

3
我有一个多元文化的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的新手,有人可以发布一些代码来实现吗?
谢谢。
2个回答

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");


给投反对票的人?为什么在这个回答发布两年后才进行投票,至少您可以提供一条评论吗?还要记住这是MVC版本2! - Rippo

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接