ASP.NET MVC 中到不同默认操作的多个默认路由
我有多个具有不同操作的控制器(没有“索引”操作)。我认为“默认”操作的操作名称不同。 我想为其名称创建默认路由,并且如果路由中仅提供了控制器名称,则执行第一个可用操作(来自我的默认操作列表)。
因此,例如,我有以下操作,我想考虑默认,并希望按以下顺序检查它们在控制器中是否存在:
List()
Draw()
ViewSingle()
路由应该以某种方式搜索 /{controller} ,然后采用上面列表中的第一个可用操作作为默认操作,例如:
/ControllerA -> ControllerA.List()
/ControllerB -> ControllerB.Draw()
/ControllerC -> ControllerC.ViewSingle()
/ControllerD -> ControllerD.Draw()
/ControllerE -> ControllerE.List()
这可能吗?我尝试创建像这样的其他默认操作,但无法使其工作:
routes.MapRoute("Default1", "{controller}/{action}",
new { controller = UrlParameter.Optional, action = "List" }
routes.MapRoute("Default2", "{controller}/{action}",
new { controller = UrlParameter.Optional, action = "Draw" }
routes.MapRoute("Default3", "{controller}/{action}",
new { controller = UrlParameter.Optional, action = "ViewSingle" }
帮助?
I have multiple controllers with different actions (no "Index" actions). The actions I consider "default" actions, are named differently. I want to create default routes for their names and have the first available action (from my list of default actions) executed if only the controller name is provided in the route.
So, for example, I have the following actions which I want to consider default and want checked for their existence in a controller in this order:
List()
Draw()
ViewSingle()
The routing should somehow search for /{controller} and then take the first available action from the list above as default action, e.g.:
/ControllerA -> ControllerA.List()
/ControllerB -> ControllerB.Draw()
/ControllerC -> ControllerC.ViewSingle()
/ControllerD -> ControllerD.Draw()
/ControllerE -> ControllerE.List()
Is this possible? I tried creating additional Default actions like this but couldn't get it to work:
routes.MapRoute("Default1", "{controller}/{action}",
new { controller = UrlParameter.Optional, action = "List" }
routes.MapRoute("Default2", "{controller}/{action}",
new { controller = UrlParameter.Optional, action = "Draw" }
routes.MapRoute("Default3", "{controller}/{action}",
new { controller = UrlParameter.Optional, action = "ViewSingle" }
Help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您的默认路线有问题。这些控制器和动作参数是为了约定而存在的。如果 URL 中有控制器名称,它将路由到合适的控制器。 Index 方法也是如此。它只是为此提供了一个默认值。它们已经是可选的,这就是原因。我认为你在这里不需要路由:你可以尝试将默认函数放在 Index 方法中,它会起作用。需要明确的是:
我认为它会起作用。但是,如果您想继续创建路由,则可以像这样创建每个路由:
请注意,ControllerA 不在花括号中,它是静态文本。因此,您为每个控制器创建路由。请记住控制器命名约定。
I think you got something wrong about the default route. Those controller and action parameters are there for convention. If URL has a controller name in it, it will route to suitable controller. Same thing is true for Index methods. It just provides a default value for that.They are already optional and that's why. I think you don't need routes here: You could try to place your default functions in Index methods and it would work.Just to be clear:
I think it would work. But if you want to go on with creating routes, you create each route like this:
Notice that ControllerA is not in curly braces,it's a static text. So you create routes for each controller. Keep controller naming convention in mind.
我同意纳西尔的观点。您应该向 url 写入一个常量。 - 就像控制器名称 - 因为 MVC 无法解析此路由并且无法弄清楚您想要显示的内容。
例如。
I agree with Narsil. You should write a constant to url. -like controller name- 'cause MVC cannot resolve this routes and cannot figure out what you want to show.
eg.