2013-12-16 25 views
0

我想创建一个使用ASP MVC与以下网址网站:路线登记ASP MVC的一个简单的图书馆应用

/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和控制器注册的最佳方法?

回答

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 } 
    ); 
+0

是的,我同意两条路由注册,但是最好先用'URL = libraries/{libraryId}/{controller}/{id}/{action}'制作Controller = Books',然后'Controller = Libraries'没有那么强'URL = {controller}/{id}/{action}'。你怎么看待这件事? – Warlock