title: How can I call a WebApi method from MVC Controller Action Method?
body: <p>How can I call a Web API method from an MVC controller action method?</p>
  
  <p>This is what I am trying, this is my controller method:</p>
  
  <pre><code>public ActionResult ProductDetail(long id)
  {
      using (var client = new HttpClient())
      {
          var productDetailUrl = Url.RouteUrl(
              "DefaultApi",
              new { httproute = "", controller = "ProductDetails", id = id },
              Request.Url.Scheme
          );
          var model = client
                      .GetAsync(productDetailUrl)
                      .Result
                      .Content.ReadAsAsync&lt;ProductItems&gt;().Result;
  
          return View(model);
      }
  }
  </code></pre>
  
  <p>My Web API method:</p>
  
  <pre><code>private ProductEntities products = new ProductEntities();
  
  public IEnumerable&lt;ProductItems&gt; GetProductDetail(int ID)
  {            
     var produc = products.ExecuteStoreQuery&lt;ProductItems&gt;(
                      "GetProductDetail @ProductID ", 
                      new SqlParameter("@ProductID", ID)); 
  
     return produc;
  }
  </code></pre>
  
  <p>When I am doing this I am getting an error @ <code>var model</code> in my MVC action method after returning the data, saying:</p>
  
  <blockquote>
    <p>"<code>Newtonsoft.Json.JsonSerializationException</code>: Cannot deserialize JSON
    array "(i.e. <code>[1,2,3]</code>) into type '<code>ProductDetails.Models.ProductItems</code>'.
    The deserialized type must be an array or implement a collection
    interface like <code>IEnumerable</code>, <code>ICollection</code> or <code>IList</code>. To force JSON arrays
    to deserialize add the <code>JsonArrayAttribute</code> to the type. Line 1,
    position 1."</p>
  </blockquote>
  
  <p>Can anyone help me do this, suggest another better method, or correct me if I am doing it wrong anywhere? I have to show the returned data in my view, the data should be returned to my controller.</p>