MVC4 Route with Id and Index Action

asp.net-mvc-4, routes

Solution

Your route would normalize out to domain.com/Customer/Index/1. When you have subsequent parts of the route, you can't eliminate an earlier component just because its value will be the default. In this case, it's looking for an action named "1" which it can't find.

Edit:

If your desired route is domain.com/Customer/ID, then you can add such a route to your route table:

routes.MapRoute(
    name: "CustomerAll",
    url: "Customer/All",
    defaults: new { controller = "Customer", action = "All" }
);
routes.MapRoute(
    name: "CustomerByID",
    url: "Customer/{id}",
    defaults: new { controller = "Customer", action = "Index" }
);

These more-specific routes should come before your default route.

Problem

I'm trying to accomplish the following and receiving the "resource not found" error for #2. Assuming because the routing is not configured correctly. Desired URLs: 1) domain.com/Customer 2) domain.com/Customer/1 *Does Not Work 3) domain.com/Customer/All ``` public ActionResult Index(int? id) { var viewModel = new CustomerViewModel(); if (!id.HasValue) id = 1; // ToDo: Current Logged In Customer Id viewModel.Load(id.Value); return View(viewModel); } public ActionResult All() { return View(CustomerModel.All()); } ``` My Route Config has the default route setup and I've tried adding an additional route to no avail. ``` routes.MapRoute( name: "Customer", url: "{controller}/{action}/{id}", defaults: new { controller = "Customer", action = "Index", id = UrlParameter.Optional } ); ``` I've excluded my attempts at setting up a new route since it doesn't work.

Original source