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

后端 未结 2 1092
孤独总比滥情好
孤独总比滥情好 2020-12-08 05:06

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

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-08 05:34

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

提交回复
热议问题