ASP.NET MVC结构设计问题

时间:2009-04-28 18:12:51

标签: asp.net-mvc asp.net-mvc-routing

我是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

默认的mapRoute适用于大多数部分:

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

当我开始使用'category'而不是产品的id时,问题就开始了......
我应该'hardcode'routeUrls,例如“产品/类别/ {id}”?

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

2 个答案:

答案 0 :(得分: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 = ""})

至于具有两个具有相同名称的控制器,是的,您可以使用不同的命名空间并在mapRoute方法中指定它们。有一个重载需要string[] namespaces。只需确保在路由时为控制器指定名称空间。

答案 1 :(得分: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

您的Index方法可以采用可为空的int(int?Id),其中Id对应于定义的产品。如果Id.HasValue为false,则返回ListCategories结果。


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

这可以使您的其他路由更清晰。