title: How can I pass a viewmodel to a webapi method from a controller
body: <p>I have a report to email to the user (since it takes some time to create.)  I am unable to call the web method and pass a viewmodel.  </p>
  
  <p>This is the WebApi controller:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Data;
  using System.Linq;
  using System.Net;
  using System.Net.Http;
  using System.Web.Http;
  
  namespace RecognitionsMVC.Controllers
  {
      public class WebAPIController : ApiController
      {
          [ActionName("Post")]
          public static void GetAllRecognitionsBySupervisorAll([FromUri]ViewModels.AllRecognitionsbyAllSupervisors AllRByAllS)
          {
              DataSet ds = Classes.Recognitions.GetAllRecognitionsBySupervisorAll(AllRByAllS.BeginDate, AllRByAllS.EndDate, AllRByAllS.RecognizedOrSubmitted);
              Classes.DataHelper.SendMeExcelFile(ds, "GetAllRecognitionsBySupervisorAll", AllRByAllS.AuthenticatedUser);
          }
  
      }
  }
  </code></pre>
  
  <p>This is the View Model I am using:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace RecognitionsMVC.ViewModels
  {
      public class AllRecognitionsbyAllSupervisors
      {
          public DateTime BeginDate { get; set; }
          public DateTime EndDate { get; set; }
          public string AuthenticatedUser { get; set; }
          public bool RecognizedOrSubmitted { get; set; }
  
          public AllRecognitionsbyAllSupervisors(DateTime BeginDate, DateTime EndDate, string AuthenticatedUser, bool RecognizedOrSubmitted)
          {
              this.BeginDate = BeginDate;
              this.EndDate = EndDate;
              this.AuthenticatedUser = AuthenticatedUser;
              this.RecognizedOrSubmitted = RecognizedOrSubmitted;
          }
  
      }
  
  }
  </code></pre>
  
  <p>and this is the Test Controller I am trying to call the WebAPI controller and pass the view model:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Data;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  
  namespace RecognitionsMVC.Controllers
  {
      public class TestController : Controller
      {
          // GET: Test
          public ActionResult Index()
          {
              DateTime BeginDate = new DateTime(2015, 1, 1);
              DateTime EndDate = new DateTime(2015, 12, 31);
              string AuthenticatedUser = "123473306";
              bool RecognizedOrSubmitted = true;
  
              string VPath = "api/WebAPI/GetAllRecognitionsBySupervisorAll";
              ViewModels.AllRecognitionsbyAllSupervisors  AllRByAllS = new ViewModels.AllRecognitionsbyAllSupervisors(BeginDate, EndDate, AuthenticatedUser, RecognizedOrSubmitted);
              return View(VPath, AllRByAllS);
  
        }
      }
  }
  </code></pre>
  
  <p>Finally this is the WebApiConfig.cs in the App_Start folder:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web.Http;
  
  namespace RecognitionsMVC
  {
      public static class WebApiConfig
      {
          public static void Register(HttpConfiguration config)
          {
              // TODO: Add any additional configuration code.
  
              // Web API routes
              config.MapHttpAttributeRoutes();
  
              config.Routes.MapHttpRoute(
                  name: "DefaultApi",
                  routeTemplate: "api/{controller}/{id}",
                  defaults: new { id = RouteParameter.Optional , action = "DefaultAction"}
              );
  
              // WebAPI when dealing with JSON &amp; JavaScript!
              // Setup json serialization to serialize classes to camel (std. Json format)
              var formatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
              formatter.SerializerSettings.ContractResolver =
                  new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
  
          }
      }
  }
  </code></pre>
  
  <p>The error indicates the api cannot be found and appears to be looking in all the view folders and not the WebAPI controller.   How can I call the WebAPI method and pass a view model?</p>