This was relatively straightforward, once I got past a simple issue.
By using a combination of [RoutePrefix("")]
& [Route("")]
on my controller, I was able to make this work. Attribute Routing requires a call to routes.MapMvcAttributeRoutes()
in your Global.asax.cs RegisterRoutes()
method.
[RoutePrefix("main/dummy")]
[Route("{action=index}/{id:long?}")]
{action=index}
defines the action handling for the route, and specifies /index as the default action, if it's not supplied (ie, ~/main/dummy/ will render ~/main/dummy/index{id:long?}
specifies the id route attribute:long
is the syntax for constraining the param to thelong
datatype?
denotes this param is optional. (more on this here)
This could also be done using the standard MapRoute()
technique:
routes.MapRoute(
name: "DummyControllerRoute",
url: "main/dummy/{action}/{id}",
defaults: new { controller = "Dummy", action = "Index", id = UrlParameter.Optional });
I forgot to add what my simple issue was.. I had 2 actions in the controller:
Index()
and Index(id)
, which would always result in the AmbiguousRouteException
, and was leading me to believe my Route was defined incorrectly. Changing that to View(id)
solved it. (I guess technically the route was wrong, but I didn't need to spend any more time trying to make it work that way)