title: ASP.NET MVC Routing using ID or action name
body: <p>I have an ASP.Net application which has an area called 'Customers'. This area has a controller with the same name with a single method called Index.</p>
  
  <p>I have the following route defined:</p>
  
  <pre><code>context.MapRoute(null,
      "Customers/{controller}/{action}",
      new { controller = "customers", action = "Index" }
  );
  </code></pre>
  
  <p>This allows my to navigate to use the following URL to navigate to the index method on my Customers controller.</p>
  
  <blockquote>
    <p>MyDomain/Customers</p>
  </blockquote>
  
  <p>In my customer area I also have another controller called products. This has a number of methods that allow me to work with product entities (mostly auto-generated by Visual Studio at the moment).</p>
  
  <p>With my current route I can navigate to the products controller using URL's like this:</p>
  
  <blockquote>
    <p>MyDomain/Customers/Products (shows the index page of the products
    controller) MyDomain/Customers/Products/Create (Shows a page to add
    new products). MyDomain/Customers/Products/Details?id=1234 (Show the
    product with the id of 1234)</p>
  </blockquote>
  
  <p>Now what I want to be able to do is navigate to the details page with a much more user friendly URL such as:</p>
  
  <blockquote>
    <p>MyDomain/Customers/Products/1234</p>
  </blockquote>
  
  <p>I have defined a new route that look like this:</p>
  
  <pre><code>context.MapRoute(null,
      "Customers/Products/{id}",
      new { controller = "Products", action = "Details" }
      );
  </code></pre>
  
  <p>The route is defined <strong>before</strong> the first route I demonstrated.
  This allows me to navigate to the products page as I want, however I can no longer navigate to other methods on my products controller.</p>
  
  <p>E.G. The following URL</p>
  
  <blockquote>
    <p>MyDomain/Customers/Products/Create</p>
  </blockquote>
  
  <p>Gives me the following error:</p>
  
  <blockquote>
    <p>The parameters dictionary contains a null entry for parameter 'id' of
    non-nullable type 'System.Int32' for method 'System.Web.Mvc.ViewResult
    Details(Int32)'</p>
  </blockquote>
  
  <p>If I change the order of the routes then I can navigate to all the methods on my products controller but my details URL reverts to the old format of having a query parameter.</p>
  
  <p>If I update the route to look like this:</p>
  
  <pre><code>context.MapRoute(null,
      "Customers/Products/{id}",
      new { controller = "Products", action = "Details", id = UrlParameter.Optional }
      );
  </code></pre>
  
  <p>Then I still get the same problem.</p>
  
  <p>Can anyone tell me how to structure my routes to get the result I want? In summary:</p>
  
  <ol>
  <li>If I navigate to the Customers area I want my URL to look like 'MyDomain/Customers'</li>
  <li>If I navigate to the product details page I want my URL to look like "MyDomain/Customers/Products/1234".</li>
  <li>If I navigate to any other product page I want my URL to look like 'MyDomain/Customers/Products/Create'</li>
  </ol>