title: How do you read POST data in an ASP.Net MVC 3 Web API 2.1 controller?
body: <p>This does not seem to be as easy as I thought. I found some solutions on the web, but they are not working for me. I have an ASP.Net MVC 3 project with the Microsoft ASP.Net Web API 2.1 nuget package installed. Now, I want to be able to read data posted to a web api controller. The data sent will vary, so I cannot used a strongly typed ViewModel.</p>
  
  <p>Here are the solutions I tried:</p>
  
  <pre><code>    public void Post([FromBody]string value)
      {
          ...
      }
  
  
      public void Post([FromBody]List&lt;string&gt; values)
      {
          ...
      }
  
  
      public void Post([FromBody]NameValueCollection values)
      {
          ...
      }
  </code></pre>
  
  <p>But my value or values variables are always empty. I know the controller is receiving data however because I can check it by accessing (System.Web.HttpContextWrapper)Request.Properties["MS_HttpContext"].Request.Form. It does not look like the proper way to retrieve the data though. There ought to be a cleaner way.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Here is how I am posting the information:</p>
  
  <p>I am posting the data from another controller in the same web application:</p>
  
  <pre><code>    public ActionResult SendEmailUsingService()
      {
          dynamic email = new ExpandoObject();
  
          email.ViewName = "EmailTest";
          email.From = "fromaddress@yahoo.com";
          email.To = "toaddress@gmail.com";
          email.Fullname = "John Smith";
          email.Url = "www.mysite.com";
  
          IDictionary&lt;string, object&gt; data = email;
  
          using (var wb = new WebClient())
          {
              string url = BaseUrlNoTrailingSlash + Url.RouteUrl("DefaultApi", new { httproute = "", controller = "Emailer" });
               var response = wb.UploadValues(url, "POST", data.ToNameValueCollection());
          }
  
          return View();
      }
  </code></pre>
  
  <p>And here is what I am getting in my Post web api controller if I declare an httpContext variable like this:</p>
  
  <pre><code>var httpContext = (System.Web.HttpContextWrapper)Request.Properties["MS_HttpContext"];
  </code></pre>
  
  <blockquote>
    <p>httpContext.Request.Form =
    {ViewName=EmailTest&amp;From=fromaddress%40yahoo.com&amp;To=toaddress%40gmail.com&amp;Fullname=John+Smith&amp;Url=www.mysite.com}</p>
  </blockquote>
  
  <p>httpContext.Request.Form is a System.Collections.Specialized.NameValueCollection {System.Web.HttpValueCollection}</p>