title: Return either xml or json from MVC web api based on request
body: <p>Given the following webapiconfig;</p>
  
  <pre><code>config.Routes.MapHttpRoute(
              name: "DefaultApi",
              routeTemplate: "api/{controller}/{id}",
              defaults: new { id = RouteParameter.Optional }
          );
  </code></pre>
  
  <p>and this controller;</p>
  
  <pre><code>  public class ProductsController : ApiController
      {
           Product[] _products = new Product[] 
          { 
              new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 }, 
              new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M }, 
              new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M } 
          };
  
          public IEnumerable&lt;Product&gt; GetAllProducts()
          {
              return _products;
          }
      }
  </code></pre>
  
  <p>Using the URL <code>http://localhost/api/Products</code> I get a list of products in XML format.</p>
  
  <p>What I would like to do is have the option to return either json or xml based on the request. So for json, it would be;</p>
  
  <pre><code>http://localhost/api/Products.json
  </code></pre>
  
  <p>and for XML, it would be;</p>
  
  <pre><code>http://localhost/api/Products.xml
  </code></pre>
  
  <p>likewise;</p>
  
  <pre><code>http://localhost/api/Products.json/1/
  http://localhost/api/Products.xml/1/
  </code></pre>
  
  <p>Is this possible and how would I achieve this functionality?</p>
  
  <p>An alternative would be something like;</p>
  
  <pre><code>http://localhost/api/json/Products/
  </code></pre>