Search code examples
asp.net-mvcasp.net-mvc-routing

Redirect with ASP.NET MVC MapRoute


On my site, I have moved some images from one folder to another.

Now, when I receive a request for old images '/old_folder/images/*' I want to make a permanent redirect to new folder with these images '/new_folder/images/*'

For example:

/old_folder/images/image1.png => /new_folder/images/image1.png

/old_folder/images/image2.jpg => /new_folder/images/image2.jpg

I have added a simple redirect controller

public class RedirectController : Controller
{
    public ActionResult Index(string path)
    {
        return RedirectPermanent(path);
    }
}

Now I need to setup proper routing, but I don't know how to pass the path part to the path parameter.

routes.MapRoute("ImagesFix", "/old_folder/images/{*pathInfo}", new { controller = "Redirect", action = "Index", path="/upload/images/????" }); 

Thanks


Solution

  • I would do in next way

    routes.MapRoute("ImagesFix", "/old_folder/images/{path}", new { controller = "Redirect", action = "Index" }); 
    

    and in controller like that

    public class RedirectController : Controller
    {
        public ActionResult Index(string path)
        {
            return RedirectPermanent("/upload/images/" + path);
        }
    }