2014-02-11 29 views
1

如何添加一條路線來捕獲指向控制器的所有URL,以便將控制器名稱後的所有參數轉換爲單個參數字符串?如何添加一個路徑到MVC3轉換部分的URL到一個參數?

我需要這種映射:

URL /Image/foo.png  -> Controller: Image, Action: Index, argument "foo.png" 
URL /Image/A/foo.png -> Controller: Image, Action: Index, argument "/A/foo.png" 
URL /Image/A/B/foo.png -> Controller: Image, Action: Index, argument "/A/B/bar.png" 

這是我的內ImageController

public ImageResult Index(string file) 
{ 
    // foo bar 
} 

行動這是我試過

routes.MapRoute(null, "Image/{file*}", new { controller = "Image", action = "Index" }); 

URL /Image/baz.png作品,文件是「 baz.png」。網址/Image/A/foo.png不起作用,我的操作不受影響。

回答

3

你非常接近。它應該是

"Image/{*file}" 

這樣file將包含以下Image/在URL everyting。

+0

太棒了,謝謝。我喜歡它,當我可以用一個角色提交來解決問題時:D。 – Nenotlep

相關問題