Attribute routing in MVC 5 and optional defaults

Viewed 6386

Traditional routing defaults meant we were able to access these URLs and always end up on the same action:

/
/Home
/Home/Index

But today we would be writing something in these lines:

[RoutePrefix("Home")]
[Route("{action=Index}")]
public class HomeController
{
    public ActionResult Index() {}

    public ActionResult ...
}

But this routing definition is by no means the same.

/           (fails)
/Home       (works)
/Home/Index (works)

So if we then change upper code to

[RoutePrefix("Home")]
[Route("{action=Index}")]
public class HomeController
{
    [Route("~/")]
    public ActionResult Index() {}

    public ActionResult ...
}

But then we turn the processing upside down:

/           (works)
/Home       (fails)
/Home/Index (fails)

We could make declarative code more verbose and make it work as the old-fashioned routing mechanism by:

[RoutePrefix("Home")]
[Route("{action=Index}")]
public class HomeController
{
    [Route("~/")]
    [Route("~/Home")]
    [Route("~/Home/Index")]
    public ActionResult Index() {}

    public ActionResult ...
}

This works with all three different routes.

Question

This issue is of course bound to the very application default action that defaults controller and action. It's just that I wonder whether this is the only way of doing it? Is there any less verbose code way of getting it to work as expected?

2 Answers
Related