I'm trying to set up some routing in one of my MVC area's.
I have a controller named AgentGroups
. I am trying to achieve the following:
- Remove
Index
from the URL - Supply a parameter to the Index action
- Allow all other actions to have their name in the URL and provide an optional parameter to them
So for example I'd like the following to work
/s/agentgroups < (Index action)
/s/agentgroups/1 < (Index action)
/s/agentgroups/someotheraction
/s/agentgroups/someotheraction/1
I currently have this in my RegisterArea
method:
// s/agentgroups/action
context.MapRoute(
"Suppliers_actions",
"s/{controller}/{action}/{agentgroupid}",
new { controller = "AgentGroups", agentgroupid = UrlParameter.Optional },
new { action = "^(?!Index$).*$" }
);
// s/agentgroups/
context.MapRoute(
"Suppliers_index",
"s/agentgroups/{agentgroupid}",
new { controller = "AgentGroups", action = "Index", agentgroupid = UrlParameter.Optional }
);
This works for 3 of the 4 URL examples I gave, the one that doesn't work correctly is:
/s/agentgroups/1 < (Index action)
I'm pretty sure it thinks the 1
parameter is an action name and therefore it doesn't work..? It does work however, if, I pass the parameter like a regular query string ie: ?agentgroupid=1
, but I'd like to avoid this if possible.
How can I change my routes to achieve the desired behavior?