title: OData get resource by ID and name
body: <p>I have this code sample that I'm trying to make the 3rd Get method to work, and also have <code>$expand</code> to work as explained below.</p>
  
  <p>If I ONLY have the methods <code>Get()</code> and <code>Get([FromODataUri] int key)</code>, I can call them both with these respective routes <code>/odata/Products</code> and <code>/odata/Products(1)?$expand=Vendor</code>.
  However, I get the below problems when I make the following changes.</p>
  
  <ol>
  <li><p>If I only change the parameter name <code>key</code> to <code>id</code> or anything else in method <code>Get([FromODataUri] int key)</code>, then the method is no longer called. The route call of <code>/odata/Products(1)</code> goes to the <code>Get()</code> method which always returns all collection.</p></li>
  <li><p>If I put the parameter name back to <code>key</code>, but change the method to something else e.g. <code>GetByKey([FromODataUri] int key)</code>, then the same problem above happens again.</p></li>
  <li><p>Also, if I call with <code>/odata/Products?key=1?$expand=Vendor</code>, the call goes to <code>GetByKey([FromODataUri] int key)</code>, but I don't get the Vendor expanded.</p></li>
  <li><p>And adding the 3rd method below <code>GetByName([FromODataUri] string name)</code>, I get "error 404 not found" when called with this route, <code>/odata/Products('Product 1')</code>. And if called with <code>/odata/Products?name='Product 1'</code> or <code>/odata/Products?name='Product 1'$expand=Vendor</code>, then the <code>Get()</code> method is triggered and again Vendor doesn't get expanded.</p></li>
  </ol>
  
  <p>I would appreciate your input on this.</p>
  
  <pre><code>public static class WebApiConfig
  {
      public static void Register(HttpConfiguration config)
      {
          // Web API configuration and services
  
          // Web API routes
          config.MapHttpAttributeRoutes();
  
          config.Routes.MapHttpRoute(
              name: "DefaultApi",
              routeTemplate: "api/{controller}/{action}/{id}",
              defaults: new { id = RouteParameter.Optional }
          );
  
          ODataModelBuilder builder = new ODataConventionModelBuilder();
          builder.EntitySet&lt;Product    ("Products");
          config.MapODataServiceRoute("ODataRoute", "odata", builder.GetEdmModel());
  
      }
  }
  
  public class ProductsController : ODataController
  {
      #region 
  
      ProductsContext db = new ProductsContext();
      private bool ProductExists(int key)
      {
          return db.Products.Any(p =     p.Id == key);
      }
      protected override void Dispose(bool disposing)
      {
          db.Dispose();
          base.Dispose(disposing);
      }
  
      #endregion
  
      [HttpGet]
      [EnableQuery]
      public IQueryable&lt;Product     Get()
      {
          return db.Products.AsQueryable();
      }
  
      [HttpGet]
      [EnableQuery]
      public SingleResult&lt;Product     Get([FromODataUri] int key)
      {
          IQueryable&lt;Product     result = db.Products.Where(p =     p.Id == key).AsQueryable();
          return SingleResult.Create(result);
      }
  
      [HttpGet]
      [EnableQuery]
      public SingleResult&lt;Product     GetByName([FromODataUri] string name)
      {
          IQueryable&lt;Product     result = db.Products.Where(p =     p.Name == name).AsQueryable();
          return SingleResult.Create(result);
      }
  }
  
  public class Product
  {
      public int Id { get; set; }
  
      public string Name { get; set; }
  
      public decimal Price { get; set; }
  
      public string Category { get; set; }
  
      public Vendor Vendor { get; set; }
  
  }
  
  public class Vendor
  {
      public int Id { get; set; }
  
      public string Name { get; set; }
  
  }
  
  public class ProductsContext : IDisposable
  {
      public ICollection&lt;Product     Products = new List&lt;Product    ()
      {
          new Product()
          {
              Category = "A",
              Id = 1,
              Name = "Product 1",
              Price = 10,
              Vendor = new Vendor()
              {
                  Id = 1,
                  Name = "Vendor 1"
              }
          },
  
          new Product()
          {
              Category = "A",
              Id = 2,
              Name = "Product 2",
              Price = 15,
              Vendor = new Vendor()
              {
                  Id = 1,
                  Name = "Vendor 1"
              }
          },
  
          new Product()
          {
              Category = "B",
              Id = 3,
              Name = "Product 3",
              Price = 10,
              Vendor = new Vendor()
              {
                  Id = 2,
                  Name = "Vendor 2"
              }
          },
      };
  
      public void Dispose()
      {
  
      }
  }
  </code></pre>