2014-01-18 30 views
8

在我的網站上,我將一些圖片從一個文件夾移到另一個文件夾。使用ASP.NET MVC重定向MapRoute

現在,當我收到舊圖像的攝影請求「/ old_folder /圖片/ *」我想打一個永久重定向到新的文件夾,這些圖片「/ new_folder /圖片/ *」

例如:

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

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

我添加了一個簡單的重定向控制器

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

現在我需要設置正確的路由,但我不知道如何傳遞路徑部分的路徑參數。

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

感謝

回答

21

我會做在接下來的方式

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

,並在控制器一樣,

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

首先下載並從this link安裝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); 

瞭解更多信息請查看以下鏈接 Redirecting Routes To Maintain Persistent URLs

+0

你應該張貼此作爲評論 –