301重定向在Asp.Net MVC [英] 301 Redirect in Asp.Net MVC

查看:123
本文介绍了301重定向在Asp.Net MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an Multiculture MVC2 website. Actually my home page can be accessed with the following paths:

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重定向到以下内容:

What I want is that all the above paths make a 301 redirect to the following:

http://mydomain.com/en

,这样我就不必不同的URL之间共享的PageRank。

so that I don't have to share pagerank between different urls.

注意,连接字符串是动态的,并设置文化为网站。

Note that the en string is dynamic and sets the culture for the website.

我Asp.Net MVC是新的,有人可以张贴一些code这样做呢?
谢谢

I'm new in Asp.Net MVC, someone could post some code to do that? Thanks

推荐答案

像这样

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

return new PermanentRedirectResult("/myurl");

这篇关于301重定向在Asp.Net MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆