C#ASP .Net MVC 4可变长度路由映射

Ale*_*ecu 3 c# asp.net-mvc asp.net-mvc-routing

我需要在asp.net mvc 3中进行可变长度路由映射.根据我的理解,我可以映射这样的路由:

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

但是如何添加可变数量的参数.我的应用程序中的路由具有从基本地址开始的目录结构,如下所示:www.mywebsite.com/root/

root后面跟的东西有一个像这样的目录结构:

www.mywebsite.com/root/directory1/directory2/image.jpg

这将对应于这样的映射:

routes.MapRoute(
            "Default", // Route name
            "root/{path}", // URL with parameters
            new { controller = "DirectoryBrowser", action = "Browse" } );
Run Code Online (Sandbox Code Playgroud)

但我不知道如何设置路径参数.

SLa*_*aks 9

您可以使用通配符参数: "root/{*path}"

  • @Alecu:`string path`. (2认同)