如何添加一个路径到MVC3转换部分的URL到一个参数?
问题描述:
如何添加一条路线来捕获指向控制器的所有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
不起作用,我的操作不受影响。
答
你非常接近。它应该是
"Image/{*file}"
这样file
将包含以下Image/
在URL everyting。
太棒了,谢谢。我喜欢它,当我可以用一个角色提交来解决问题时:D。 – Nenotlep