5

「Customers」という領域を持つ ASP.Net アプリケーションがあります。この領域には、Index という 1 つのメソッドを持つ同じ名前のコントローラーがあります。

次のルートが定義されています。

context.MapRoute(null,
    "Customers/{controller}/{action}",
    new { controller = "customers", action = "Index" }
);

これにより、次の URL を使用して Customers コントローラーの index メソッドに移動できます。

マイドメイン/顧客

私の顧客エリアには、製品と呼ばれる別のコントローラーもあります。これには、製品エンティティ (現時点ではほとんどが Visual Studio によって自動生成されます) を操作できるようにするいくつかのメソッドがあります。

現在のルートでは、次のような URL を使用して製品コントローラーに移動できます。

MyDomain/Customers/Products (製品コントローラーのインデックス ページを表示) MyDomain/Customers/Products/Create (新しい製品を追加するページを表示)。MyDomain/Customers/Products/Details?id=1234 (ID が 1234 の製品を表示)

今私ができるようにしたいのは、次のようなはるかにユーザーフレンドリーな URL を使用して詳細ページに移動することです。

MyDomain/顧客/製品/1234

次のような新しいルートを定義しました。

context.MapRoute(null,
    "Customers/Products/{id}",
    new { controller = "Products", action = "Details" }
    );

ルートは、私が示した最初のルートのに定義されています。これにより、必要に応じて製品ページに移動できますが、製品コントローラーの他のメソッドに移動できなくなりました。

EG下記URL

MyDomain/Customers/Products/Create

次のエラーが表示されます。

パラメーター ディクショナリには、メソッド 'System.Web.Mvc.ViewResult Details(Int32)' の null 非許容型 'System.Int32' のパラメーター 'id' の null エントリが含まれています。

ルートの順序を変更すると、製品コントローラーのすべてのメソッドに移動できますが、詳細 URL はクエリ パラメーターを持つ古い形式に戻ります。

ルートを次のように更新すると:

context.MapRoute(null,
    "Customers/Products/{id}",
    new { controller = "Products", action = "Details", id = UrlParameter.Optional }
    );

その後、私はまだ同じ問題を抱えています。

希望する結果を得るためにルートを構築する方法を誰か教えてもらえますか? 要約すれば:

  1. Customers エリアに移動する場合、URL を「MyDomain/Customers」のようにしたい
  2. 製品の詳細ページに移動する場合、URL を「MyDomain/Customers/Products/1234」のようにしたいと考えています。
  3. 他の製品ページに移動する場合、URL を「MyDomain/Customers/Products/Create」のようにしたい
4

4 に答える 4

4

ID が常に int になる場合は、次のようにルートに制約を追加できます。

context.MapRoute(null,
                 "Customers/Products/{id}",
                 new {controller = "Products", action = "Details", id = UrlParameter.Optional},
                 new {id = @"\d+"} // Constraint to only allow numbers
                );
于 2012-08-22T10:04:42.233 に答える
1

次のようなことを試してください:

public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
        "Products_Details",
        "Customers/Products/{id}",
        new { controller="Products", action="Details" },
        new { id = @"\d+" }
    );

    context.MapRoute(
        "Customers_default",
        "Customers/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional }
    );
}

更新:ルートに制約を追加しました。

説明: 最初のルートは詳細ページにマップされ、ユーザーに数値形式の ID (最初の の最後のパラメーターMapRoute) を提供するように強制します。ID にフォーマットがない場合\d+、ルートと一致せず、デフォルトが使用されます。これにより、次のルートが得られます。

Customers\Products (index)
Customers\Products\1234 (details of 1234)
Customers\Products\Create (create page for products)
Customers\Products\Edit\1234 (edit page for 1234)
于 2012-08-22T10:12:33.943 に答える
0

既存のルートを維持したい場合は、これを試してください:

context.MapRoute(null,
     "Customers/Products/{id}", // id is optional, so Customers/Products might reasonably return all products
     new { controller = "Products", action = "Details", id = UrlParameter.Optional },
     new {id = @"\d+"} // must be numeric if present
);

context.MapRoute(null,
     "Customers/Products/{action}/{id}", // Id is optional, so this needs an action.
     new { controller = "Products", action = "Details", id = UrlParameter.Optional }
     // maybe the id is not numeric for all actions? I don't know so I won't constrain it.
);

context.MapRoute(null,
     "Customers/{controller}/{action}", // Default Customers route
     new { controller = "customers", action = "Index" }
);

最初の 2 つは、1 つまたは 2 つの他の部分と同様に URL を処理し/Customers/Products/...、最後の 1 つは、で始まる他のすべてを処理します。/Customers/

于 2012-08-22T10:12:38.720 に答える
0

これを試して

    context.MapRoute(
                    "Customers_Products",
                    "Customers/Products/{productId}",
                    new { controller = "Products", action = "Details", productId= UrlParameter.Optional }
                );

    context.MapRoute(null,
                    "Customers/{controller}/{action}",
                     new { controller = "customers", action = "Index" }
);
于 2012-08-22T10:20:44.060 に答える