質問

次のシナリオを処理する方法を見つけようとしています。一般に、テーブルには大量のレコードがあります。これらすべてには、ツリーを形成するための ID フィールドと ParentID フィールドがあります。

Page1
 - Page2
 - Page3
Page4
 - Page5
 -- Page6

さて、Page3 と Page6 のルートを次のようにしたいと思います。 /Page1/Page6 そして /Page3/Page5/Page6 それぞれ。つまり、URL にすべての親を含めたいと考えています。

上記の結果を達成するためにコントローラーのアクション/ルーティングを設定するにはどうすればよいですか?

編集:言い忘れていましたが、上記の構造は動的です - ノードは追加/削除/親の変更などが可能です。

役に立ちましたか?

解決

ワイルドカード一致を使用できます。

可能なルート:

routes.MapRoute("SomeName", "{*Page}", new { controller = "ControllerName", action = "ActionName" });

そしてアクションで文字列Pageを受け入れ、おそらく分割して手動で解析しますか?

これも役立つ場合があります: URLルーティング

他のヒント

コントローラー名とアクションは固定されており、URL に基づいていないことに注意してください。

// place the more specific route first
routes.MapRoute("r1", "{parent}/{child}/{subchild}", 
    new { controller = "Page", action = "Index", parent = parent, child = child, subchild = subchild });

routes.MapRoute("r2", "{parent}/{child}", 
    new { controller = "Page", action = "Index", parent = parent, child = child });


public class PageController
{
    public ActionResult Index(string parent, string child, string? subchild)
    {
        // stuff
    }
}

使用する必要があります

routes.MapRoute("Page6", "Page3/Page5/Page6", new { controller = "Page6", action = "Index" });

または正規表現ルーティングを使用する

http:/ /blog.sb2.fr/post/2009/01/03/Regular-Expression-MapRoute-With-ASPNET-MVC.aspx

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top