0

MVC4のルートで問題が発生しています。

特定の製品の外部に存在するアクションがいくつかあり、ユーザーが選択した製品の内部に存在するアクションがさらに多くあります。アクションに対応するために、2つのルートをマッピングしました

        context.MapRoute(
            "CMS_product",
            "CMS/{productId}/{controller}/{action}/{id}",
            new { controller = MVC.CMS.Home.Name, action = MVC.CMS.Home.ActionNames.Index, productId = default(Guid).ToString(), id = UrlParameter.Optional },
            new string[] { "Areas.CMS.Controllers" }
        );

        context.MapRoute(
            "CMS_default",
            "CMS/{controller}/{action}/{id}",
            new { controller = MVC.CMS.Home.Name, action = MVC.CMS.Home.ActionNames.Index, id = UrlParameter.Optional },
            new string[] { "Areas.CMS.Controllers" }
        );

したがって、これはジェネリックで機能しますが、私のルートはいずれもデフォルトルートと一致しなくなり、次のようなURLを取得する代わりになります

〜/CMS/製品/リスト

製品の外部で操作すると、次のようなURLが表示されます。

〜/ CMS / 00000000-0000-0000-0000-000000000000 / Product / List

別の注意:Prodcut / Listをルートとしてハードコーディングしようとしましたが、他のURLの前に一致することを期待して、CMS_productの前に配置しました。シンプルなものを見落としているような気がします。

4

2 に答える 2

1

完全を期すために、他の誰かが同様の問題に遭遇した場合、ここで解決策があります。

       // used to match ~/CMS/00000000-0000-0000-0000-000000000000/Product/List 
       // prevents the GUID.Empty from showing when there is no product value
       // in the segment
       context.MapRoute(
            name: "CMS_nullproduct",
            url: "CMS/{controller}/{action}/{id}",
            defaults: new { controller = MVC.CMS.Home.Name, action = MVC.CMS.Home.ActionNames.Index, id = UrlParameter.Optional },
            constraints: new { productId = Guid.Empty.ToString() },
            namespaces: new string[] { "Areas.CMS.Controllers" }
        ); 

        // matches any route with a productId segment value of anything aside from
        // GUID.Empty
        context.MapRoute(
            name: "CMS_product",
            url: "CMS/{productId}/{controller}/{action}/{id}",
            defaults: new { controller = MVC.CMS.Home.Name, action = MVC.CMS.Home.ActionNames.Index, id = UrlParameter.Optional },
            constraints: new { productId = @"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$" },
            namespaces: new string[] { "Areas.CMS.Controllers" }
        );

        context.MapRoute(
            name: "CMS_default",
            url: "CMS/{controller}/{action}/{id}",
            defaults: new { controller = MVC.CMS.Home.Name, action = MVC.CMS.Home.ActionNames.Index, id = UrlParameter.Optional },
            namespaces: new string[] { "Areas.CMS.Controllers" }
        );
于 2013-03-15T15:57:01.073 に答える
0

私の意見では、のデフォルト値を削除する必要がありますproductId

context.MapRoute(
        "CMS_product",
        "CMS/{productId}/{controller}/{action}/{id}",
        new { controller = MVC.CMS.Home.Name, action = MVC.CMS.Home.ActionNames.Index, id = UrlParameter.Optional },
        new string[] { "Areas.CMS.Controllers" }
    );

productIdを指定しない場合、ルーティングエンジンは2番目のルートと一致し、〜/ CMS / Product / Listを生成する必要がありますが、productIdを指定すると、最初のルールと一致します。

さらに、カスタムIRouteConstraintを記述したり、正規表現を使用してproductId値を制限したりできます。

context.MapRoute(
        "CMS_product",
        "CMS/{productId}/{controller}/{action}/{id}",
        new { controller = MVC.CMS.Home.Name, action = MVC.CMS.Home.ActionNames.Index, id = UrlParameter.Optional },
        new { productId = @"^\d{8}\-\d{4}\-\d{4}\-\d{4}\-\d{12}$" }
        new string[] { "Areas.CMS.Controllers" }
    );
于 2013-03-04T21:09:38.887 に答える