在ASP MVC中为简单的库应用程序注册路由

时间:2013-12-16 19:03:13

标签: asp.net-mvc routes controllers clean-urls

我想使用ASP MVC创建一个带有以下URL的网站:

/libraries                      ## displays a list of libraries
/libraries?pageIndex=1          ## a list of libraries with pagination
/libraries/1                    ## the library with ID == 1
/libraries/1/books              ## a list of books in the library 1
/libraries/1/books?pageIndex=0  ## a paged list of books in the library 1
/libraries/1/books/2            ## the book with ID == 2 in the library 1
/libraries/1/books/2/edit       ## edit page for the book 2 in the library 1

RouteCollections和Controllers注册的最佳方式是什么?

2 个答案:

答案 0 :(得分:1)

您可能有两条具有以下模式的路线:

{controller}/{parentId}/{item}/{id}/{action}

其中:

  • {action}是可选的,默认为index;
  • {id}是可选的

{controller}/{parentId}

其中:

  • {parentId}是可选的

您的RouteConfig.cs文件可能如下所示:

    routes.MapRoute(
        name: "libraryDetail",
        url: "{controller}/{parentId}/{item}/{id}/{action}",
        defaults: new { controller = "Libraries", action = "Index", id = UrlParameter.Optional });

    routes.MapRoute(
        name: "libraries",
        url: "{controller}/{parentId}",
        defaults: new { controller = "Libraries", action = "List", id = UrlParameter.Optional }
    );

答案 1 :(得分:0)

我刚刚发现了一篇很棒的文章,回答了我的所有问题,希望对其他人有所帮助:)

Customizing Routes in ASP.NET MVC