2009-04-28 43 views
1

我新的ASP.NET MVC和我现在面临一些结构性的设计问题。ASP.NET MVC结构设计问题

我无法弄清楚如何设置路由。

我想以下几点:

 
http://website/       > HomeController  Action=Index 

Public: 
http://website/{controller}    > SectionController Action=Index 
http://website/products/id    > ProductsController Action=Details 
http://website/products/category/id  > ProductsController Action=ListByCatId 
http://website/products/categories/  > ProductsController Action=ListCategories 
http://website/products/categories/id > ProductsController Action=DetailsCategories 

Admin: 
http://website/admin/     > AdminController Action=Index 
http://website/admin/{controller}  > SectionController Action=Index 

默认图路线是对大多数零件:

routes.MapRoute("Default", "{controller}/{action}/{id}", _ 
       New With {.controller = "Home", .action = "Index", .id = ""}) 

当我开始把“类别”,而不是产品的问题的id开始...
如果我硬编码“的routeUrls,如“产品/分类/(编号)”?

对于管理部分:
我喜欢把属于在该网站的管理部分的所有控制器:/Controllers/Admin/XxxController.vb。是否有可能命名空间并让它们与公共区域中的名称相同? e.q.
- Website.ProductsController类公共部位和
- Website.Admin.ProductsController为管理部分?我应该如何设置?

+0

*领域*(新的MVC 2)可以帮助您与此有关。它们提供了站点不同区域之间的清晰分隔,区域定义了路由,控制器等内容。 – bzlm 2010-01-27 11:01:02

回答

3

这是我会怎么做:

routes.MapRoute("ProductDetail", "Products/{id}", _ 
    New With {.controller = "Products", .action = "Details", .id = ""}, 
    New With {.id = @"\d+"}) 
    //constraint, so this route will not catch a category name 
    //or the URL below 

routes.MapRoute("ProductList", "Products/category/{id}", _ 
    New With {.controller = "Products", .action = "ListByCatId", .id = ""}) 

routes.MapRoute("Category", "Products/categories/{id}", _ 
    New With {.controller = "Products", .action= "ListCategories", .id = ""}) 
    //for the route above, let it fall to the ListCategories action 
    //and in that action take a nullable of int as a parameter. 
    //if the id parameter has a value, 
    // return DetailsCategories(id) 
    //else list the categories. 


routes.MapRoute("Default", "{controller}/{action}/{id}", _ 
    New With {.controller = "Home", .action = "Index", .id = ""}) 

至于有两个控制器具有相同的名称,是的,你可以与具有不同的命名空间,并在图路线方式识别他们做到这一点。有一个超载需要string[] namespaces。只需确保在路由时为具有相同名称的控制器指定命名空间。

1

是的,如果你需要补充的是不对应的默认附加路由,继续前进,这样做。您需要在默认路线上方添加其他自定义路线,并在添加路线时使用窄到宽的顺序(以便首先进行窄匹配)。我会建议让你的控制器动作匹配你想要首先尽可能显示的默认命名。

代替:

http://website/products/id    > ProductsController Action=Details 
http://website/products/category/id  > ProductsController Action=ListByCatId 
http://website/products/categories/  > ProductsController Action=ListCategories 
http://website/products/categories/id > ProductsController Action=DetailsCategories 
使用

http://website/product/id    > ProductsController Action=Index 
http://website/product/category/id  > ProductsController Action=category 
http://website/product/  > ProductsController Action=Index 
http://website/product/categories/id > ProductsController Action=categories 

索引方法可以采取可空INT(INT标识?),其中ID对应于定义的产品。如果Id.HasValue为false,则返回ListCategories结果。


public ActionResult Index(int? Id) 
{ 
    if (Id.HasValue) return Details(Id.Value); 
    return ListCategories(); 
} 

这可以让你额外的路由清洁。