ASP.NET MVC 4 - 301 Redirects in RouteConfig.cs

niico picture niico · Jun 7, 2013 · Viewed 29.8k times · Source

How can I add a route to the RouteConfig.cs file in an ASP.NET MVC 4 app to perform a permanent 301 redirect to another route?

I would like certain different routes to point at the same controller action - it seems a 301 would be best practice for this, specially for SEO?

Thanks.

Answer

Bastianon Massimo picture Bastianon Massimo · Jun 7, 2013

You have to use RedirectPermanent, here's an example:

public class RedirectController : Controller
{

    public ActionResult News()
    {

        // your code

        return RedirectPermanent("/News");
    }
}

in the global asax:

    routes.MapRoute(
        name: "News old route",
        url: "web/news/Default.aspx",
        defaults: new { controller = "Redirect", action = "News" }
    );