2009-04-28 8 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 

デフォルトmapRouteは、部品のほとんどの罰金です:

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

私は「カテゴリ」の代わりに、製品のIDの問題を入れて起動すると、開始...
私は 'route'rls、'ハードコードする必要があります"products/category/{id}"?管理部分について

:/Controllers/Admin/XxxController.vb:
私は中のWebサイトの管理セクションに属するすべてのコントローラを入れたいと思います。それらを名前空間にすることが可能であり、それらを公開セクションと同じ名前にすることができますか? e.q.
- Website.ProductsController public classのクラスと
- Admin.web.Admin.ProductsController for Adminセクション?これをどのようにセットアップすればよいですか?

+0

*エリア*(MVC 2で新機能)がこれを手伝ってくれるかもしれません。それらは、あなたのサイトの異なるエリア間をきれいに分けることができます。エリアでは、ルーティングやコントローラなどが定義されます。 – bzlm

答えて

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 = ""}) 

同じ名前の2つのコントローラを持つためとして、はい、あなたは別の名前空間を持つとmapRoute方法でそれらを指定してそれを行うことができます。 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 

Idが定義されている製品に対応してNULL可能int型(int型同上?)を取ることができますあなたのIndexメソッド。 Id.HasValueがfalseの場合、ListCategoriesの結果を返します。


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

これにより、ルーティングがきれいになります。

関連する問題