Asp.NET MVC 2 路由

4
我想定义一个路由,其中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"
                }
                );  
1个回答

0

你需要为所有可能的组合制定路由,这可能会有点棘手:

//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...

但是,这里也有一个问题:由于startend都是数字,所以无法确定Source/Changeset/2/fodass中的“2”是start还是end变量(因为它们是可选的),因此您可能需要想出一种新方法或将其更改为类似于Source/Changeset/Start/2/fodassSource/Changeset/End/5/fodassSource/Changeset/Start/2/End/5/fodass等内容。

如果一个值存在,那么它就是起点,所以不需要再做什么。谢谢你的帮助。 - Filipe Pinheiro

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接