Asp.NET MVC 2 路由
我想定义一个在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你需要为所有可能的组合制定路线,这可能会有点麻烦:
但是,这里也有一个问题:因为 start 和 end 都是数字,没有办法(因为它们是可选的)来决定 Source/Changeset/2/fodass 中的“2”是开始还是结束< /em> 变量,因此您可能需要想出一种新方法,或者将您的方法更改为 Source/Changeset/Start/2/fodass、Source/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:
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.