是否可以在MVC中进行动态操作

时间:2012-06-01 10:32:22

标签: asp.net-mvc-3 routes

我正在尝试使用MVC构建文件共享网站。其中一个很好的功能是短网址 - 通常采用以下格式:

http://site.com/Rtravf2
http://site.com/XddsvgF
http://site.com/AaraEwq

URL末尾的“操作”实际上是用于下载文件的ID。这种格式的网址都可以映射到家庭控制器中的相同操作方法,称为下载。

这是路线表中唯一且唯一的路线

routes.MapRoute(
    "Default", // Route name
    "{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

我应该构建什么样的路由,以便可以将这些Url映射到Home Controller上的“下载”操作。

目前正在获得一堆404(自然!)

感谢您的任何指示。

1 个答案:

答案 0 :(得分:0)

routes.MapRoute(null, // Route name
    "{id}", // URL with parameters
    new { controller = "Home", action = "Download" }
);

routes.MapRoute(null, // Route name
    "Index/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);