使用ASP.NET MVC MapRoute重定向

时间:2022-02-08 17:42:49

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/*'

现在,当我收到旧图像'/ old_folder / 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.

现在我需要设置正确的路由,但我不知道如何将路径部分传递给path参数。

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

Thanks

谢谢

2 个解决方案

#1


21  

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

#2


4  

first download and install RouteMagic package from this link , then redirect your old address to the new address Like the below code :

首先从此链接下载并安装RouteMagic包,然后将旧地址重定向到新地址如下代码:

var NewPath = routes.MapRoute("new", "new_folder/images/{controller}/{action}");
var OldPath = routes.MapRoute("new", "old_folder/images/{controller}/{action}");
routes.Redirect(OldPath ).To(NewPath );

for more information please check out the following link Redirecting Routes To Maintain Persistent URLs

有关更多信息,请查看以下链接重定向路由以维护持久URL

#1


21  

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

#2


4  

first download and install RouteMagic package from this link , then redirect your old address to the new address Like the below code :

首先从此链接下载并安装RouteMagic包,然后将旧地址重定向到新地址如下代码:

var NewPath = routes.MapRoute("new", "new_folder/images/{controller}/{action}");
var OldPath = routes.MapRoute("new", "old_folder/images/{controller}/{action}");
routes.Redirect(OldPath ).To(NewPath );

for more information please check out the following link Redirecting Routes To Maintain Persistent URLs

有关更多信息,请查看以下链接重定向路由以维护持久URL