1

私は ASP.NET MVC を初めて使用し、構造設計の問題に直面しています。

ルーティングの設定方法がわかりません。

私は次のものが欲しい:

http://website/ > HomeController Action=Index

公衆:
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 アクション=ListCategories
http://website/products/categories/id > ProductsController Action=DetailsCategories

管理者:
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 の代わりに「カテゴリ」を入力し始めると、問題が発生します...
「products/category/{id}」などの routeUrl を「ハードコード」する必要がありますか?

管理者の場合:
Web サイトの管理者セクションに属するすべてのコントローラーを /Controllers/Admin/XxxController.vb に配置したいと思います。それらに名前空間を付けて、公開セクションと同じ名前にすることは可能ですか? eq
- パブリック パーツの Website.ProductsController クラスと
- 管理セクションの Website.Admin.ProductsController ? これをどのようにセットアップすればよいですか?

4

2 に答える 2

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。ルーティング中に同じ名前のコントローラーの名前空間を指定してください。

于 2009-04-28T18:42:35.947 に答える
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 メソッドは、定義された製品に対応する Id の null 許容 int (int? Id) を取ることができます。Id.HasValue が false の場合、ListCategories の結果を返します。


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

これにより、追加のルーティングをよりクリーンに保つことができます。

于 2009-04-28T18:43:47.427 に答える