301 Redirect to Asp.Net MVC

I have a Multiculture MVC2 website. In fact, my homepage 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/

I want all of the above paths to redirect 301 to the following:

http://mydomain.com/en

so I don’t need to pass the pagerank between different urls.

Note that the line enis dynamic and sets the culture for the website.

I'm new to Asp.Net MVC, can someone post code for this? Thanks

+3
source share
2 answers

You can create a custom action result. See this topic: 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

Source: https://habr.com/ru/post/1781020/


All Articles