没有(或默认)操作名称的MVC路由 [英] MVC Routing with no (or a default) action name
问题描述
使用MVC 5,我试图通过路由来获得一些友好的控制器/动作名称.以此将我的头撞在墙上.
Using MVC 5, I am trying to get some friendly controller/action names working through routing. Banging my head against the wall with this.
使用此控制器:
MyController
Index(startPage, pageSize)
EditAction(itemId)
我正在使用两条路由(在继承的AreaRegistration类下定义)
I am using two routes (defined under an inherited AreaRegistration class)
context.MapRoute(
"MyControllerRoute",
"myArea/MyController/{action}/{itemId}",
new
{
controller = "MyController",
action = "Index",
itemId = UrlParameter.Optional
},
new[] {"MySite.Areas.MyArea.Controllers"});
context.MapRoute(
"MyControllerRoutePaged",
"myArea/MyController/{action}/{startPage}/{pageSize}",
new
{
controller = "MyController",
action = "Index",
pageSize = UrlParameter.Optional,
startPage = UrlParameter.Optional
},
new[] {"MySite.Areas.MyArea.Controllers"});
我想实现以下链接:
mysite.com/MyArea/MyController/startPage/pageSize
mysite.com/MyArea/MyController/EditAction/itemId
mysite.com/MyArea/MyController2/ (this being the Index action for a given controller)
mysite.com/MyArea/MyController3/ (this being the Index action for another controller)
mysite.com/MyArea/MyController/ (mysite.com/MyArea/MyController/0/20 would also work if paging existis on the controller)
mysite.com/MyArea/MyController/5/20
mysite.com/MyArea/MyController/EditAction/5
分页链接显示在默认的索引"页面上.不在任何操作页面"上.特别是,我希望隐藏索引"操作名称,因此最终不会出现类似这样的链接:
The paging links appear on the default Index page. Not on any "action pages". In particular I am looking to hide the Index action name, so I don't end up with links like:
mysite.com/MyArea/MyController/MyController/.../
如果我在路由中放置{action}占位符,但路由最终发生冲突,则可以实现此目的.我尝试使用ActionLink和RouteLink(定义路由名称),但是总有一个路由首先匹配,然后中断下一个.如果我更改路线的顺序,这种情况仍然会发生,但是反之亦然.
I can achieve this if I drop the {action} placeholder in the routes but the routes end up clashing. I have tried using ActionLink and RouteLink (defining the route name) but there's always one route that matches first and breaks the next one. If I change the ordering of the routes this still happens but the other way around.
我正在使用路由调试器,可以看到出现故障的地方,但是我一直绕圈走,试图使其正常运行.
I am using a route debugger and I can see where things are failing but I keep going around in circles trying to get it working.
有什么想法吗?谢谢.
推荐答案
我认为您应该使用 查看全文