簡體   English   中英

Asp.NET MVC 2路由

[英]Asp.NET MVC 2 Routing

我想在URL中間定義一個有2個可選參數的路由, 開始 結束參數是數字

routes.MapRoute(
                "",
                "Source/Changeset/{start}/{end}/{*path}",
                new {
                        controller = "Source",
                        action = "Changeset",
                        start = UrlParameter.Optional,
                        end = UrlParameter.Optional, 
                        path = "crl"
                    },
                new { start = @"\d+", end = @"\d+" }
                );

我嘗試了不同的方法,沒有一個工作,我可以使用你的一些幫助。

提前致謝。

編輯

我設法以這種方式解決問題,但它遠非優雅。

routes.MapRoute(
                "",
                "Source/Changeset/{start}/{end}/{*path}",
                new {
                        controller = "Source",
                        action = "Changeset",
                        start = UrlParameter.Optional,
                        end = UrlParameter.Optional, 
                        path = "crl"
                    },
                new { start = @"\d+", end = @"\d+" }
                );  

            routes.MapRoute(
                "",
                "Source/Changeset/{start}/{*path}",
                new
                {
                    controller = "Source",
                    action = "Changeset",
                    start = UrlParameter.Optional,
                    path = "crl"
                },
                new { start = @"\d+" }
                );  

            routes.MapRoute(
                "",
                "Source/Changeset/{*path}",
                new
                {
                    controller = "Source",
                    action = "Changeset",
                    path = "crl"
                }
                );  

你將需要為所有可能的組合制作路線,這可能會有點毛茸茸:

//route if everything is included
routes.MapRoute(null, ""Source/Changeset/{start}/{end}/{*path}",
    new { controller = "Home", action = "Index" }
);

//route if nothing is included
routes.MapRoute(null, ""Source/Changeset/{*path}",
    new { controller = "Home", action = "Index", start=0, end=5 } //defaults
);

//and so on...

但是,這里也存在一個問題:因為開始結束都是數字,所以沒有辦法(因為它們是可選的)來決定Source / Changeset / 2 / fodass中的'2'是開始還是結束變量,所以你可能需要考慮一種新方法,或者將你的方法改為Source / Changeset / Start / 2 / fodassSource / Changeset / End / 5 / fodassSource / Changeset / Start / 2 / End / 5 / fodass

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM