Asp.NET MVC 2 路由

发布于 2024-09-04 04:30:11 字数 1880 浏览 5 评论 0原文

我想定义一个在URL中间2个可选参数的路由,开始 >end 参数是数字,

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"
                }
                );  

I want to define a route that have 2 optional parameters in the middle of the URL the start an end parameters are digits

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+" }
                );

i tried diferent approaches and none of them worked, i could use some of your help.

Thanks in advance.

EDIT

I manage to solve the problem this way, but it's far from elegant.

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"
                }
                );  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

复古式 2024-09-11 04:30:11

你需要为所有可能的组合制定路线,这可能会有点麻烦:

//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”是开始还是结束< /em> 变量,因此您可能需要想出一种新方法,或者将您的方法更改为 Source/Changeset/Start/2/fodassSource/Changeset/End/5/ fodass源/变更集/开始/2/结束/5/fodass等。

you are going to need to make routes for all possible combinations, which can get a little hairy:

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

But, there is also a problem here: since start and end are both digits, there would be no way (since they are optional) to decide if the '2' in Source/Changeset/2/fodass is the start or end variable, so you may have to think up a new approach, or change yours to something like Source/Changeset/Start/2/fodass, Source/Changeset/End/5/fodass, Source/Changeset/Start/2/End/5/fodass, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文