ASP.NET MVC 路由映射问题
我正在努力在 asp.net mvc 2 中生成出站 url。这是场景。
控制器:移动控制器 动作:索引() View: Index.aspx
现在我想要的是将多个 url 映射到具有不同参数 (locationId) 值的同一控制器 (MoveController) 和操作 (Index),
例如
- 网址 -> RouteData
- /生产/ ->移动/索引/1
- /安装/ ->移动/索引/2
- /移动/3/ -> Move/Index/3
我的映射如下所示:
public static void RegisterRoutesTo(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{*favicon}", new
{
favicon = @"(.*/)?favicon.ico(/.*)?"
});
routes.MapRoute(
null, // Route name
"Production/{locationId}", // URL with parameters
new
{
controller = "Move",
action = "Index"
}, // Parameter defaults
new
{
locationId = @"\d+"
}// constraint for Production url
);
routes.MapRoute(
null, // Route name
"Installation/{locationId}", // URL with parameters
new
{
controller = "Move",
action = "Index"
}, // Parameter defaults
new
{
locationId = @"\d+"
}// constraint for Production url
);
routes.MapRoute(
null, // Route name
"Move/{locationId}", // URL with parameters
new
{
controller = "Move",
action = "Index"
}, // Parameter defaults
new
{
locationId = @"\d+"
}// constraint for Production url
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
} // Parameter defaults
);
}
现在要在我的母版页菜单中生成出站网址,我正在做类似ethis的操作
<%= Html.ActionLink("Production", "Index", "Move", new{locationId = 1}, null)%>
<%= Html.ActionLink("Installation", "Index", "Move", new{locationId = 2}, null)%>
但是上面生成的
- /Production/1
- /Production/2
是正确的,但我怎么知道 生成
- 当 locationId =1 时
- /Production/ /Installation/ 当 locationId = 2 时
有任何想法吗?
等待,
I am struggling with generating outbound urls in asp.net mvc 2. Here is the scenario.
Controller: MoveController
Action: Index()
View: Index.aspx
Now what I would like is to have multiple urls mapping to same controller (MoveController) and action (Index) with different parameter (locationId) value
e.g.
- url -> RouteData
- /Production/ -> Move/Index/1
- /Installation/ -> Move/Index/2
- /Move/3/ -> Move/Index/3
My mapping look like this:
public static void RegisterRoutesTo(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{*favicon}", new
{
favicon = @"(.*/)?favicon.ico(/.*)?"
});
routes.MapRoute(
null, // Route name
"Production/{locationId}", // URL with parameters
new
{
controller = "Move",
action = "Index"
}, // Parameter defaults
new
{
locationId = @"\d+"
}// constraint for Production url
);
routes.MapRoute(
null, // Route name
"Installation/{locationId}", // URL with parameters
new
{
controller = "Move",
action = "Index"
}, // Parameter defaults
new
{
locationId = @"\d+"
}// constraint for Production url
);
routes.MapRoute(
null, // Route name
"Move/{locationId}", // URL with parameters
new
{
controller = "Move",
action = "Index"
}, // Parameter defaults
new
{
locationId = @"\d+"
}// constraint for Production url
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
} // Parameter defaults
);
}
Now to generate the outbound urls in my master page menu, I am doing something lik ethis
<%= Html.ActionLink("Production", "Index", "Move", new{locationId = 1}, null)%>
<%= Html.ActionLink("Installation", "Index", "Move", new{locationId = 2}, null)%>
But the above generates
- /Production/1
- /Production/2
which is correct, but how can i tell it to generate
- /Production/ when the locationId =1
- /Installation/ when locationId = 2
Any Idea?
Awaiting,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可能性不大,但绝对值得一试,因为上面的路线没有表达你想要的:)
;
);
long shot, but definitely worth a shot as your routes above weren't expressing what you want:
);
);
您是对的,生产和安装路线完全相同。我只是不知道如何区分它们,因为它们都采用相同的参数 {locationId} 但具有不同的值。正如你所看到的,我提供了不同的默认值,但这只是一个默认值,因此我尝试对它们施加约束,
参数值为 null
这可行,但后来我意识到,在定义 Html.ActionLink 时,它生成的 Production/ url,它应该是订单中的第一个。
我不知道的是,不必在 url 中定义 {parameters}。我一直认为参数需要在url中定义。
感谢大家的帮助
You are correct the Production and Installation routes are exactly the same. I just didn't know how to differentiate them, as they both take the same parameter {locationId} but with different values. As you can see I have provided the default value to be different, but thats just a default value, so therefore I tried putting constraint on them
This works but then I realisez that when defining the Html.ActionLink giving value of null for parameters
it produces the Production/ url, which it should as its the first one in the order.
What I didn;t know is that one don;t necessarliy have to define the {parameters} in the url. I always thought that the parameters need to be defined in the url.
Thanks for all the help guys
提供的 ActionLink 帮助程序扩展不会执行此操作。您需要编写一个新的自定义 ActionLink 方法,该方法将根据您的需要有条件地呈现。
The provided ActionLink helper extension won't do that. You'd need to write a new, custom ActionLink method that will render conditionally as you require.