title: console app HttpClient post to mvc web api
body: <p>I have a default mvc web api instance built from the Visual Studio 2012 template.  It has the following routes and post method in the default ValuesController - the MVC site is unmodified from initial creation other than the contents of the Post method.  Also, I'm using .NET framework 4.0 since I'm planning to target Azure.</p>
  
  <p>Register method</p>
  
  <pre><code>public static void Register(HttpConfiguration config)
  {
      config.Routes.MapHttpRoute(
          name: "DefaultApi",
          routeTemplate: "api/{controller}/{id}",
          defaults: new { id = RouteParameter.Optional }
      );
  }
  </code></pre>
  
  <p>and Post method</p>
  
  <pre><code>    // POST api/values
      public string Post([FromBody]string value)
      {
          if (value != null)
          {
              return "Post was successful";
          }
          else
              return "invalid post, value was null";
      }
  </code></pre>
  
  <p>I created a Console application which uses HttpClient to simulate posting to the service, but unfortunately the "value" coming in to the Post is always null.  The Post method is successfully hit following the PostAsync call on HttpClient.</p>
  
  <p>It's not clear to me how to map my request such that value contains the StringContent I'm passing in...</p>
  
  <pre><code>    static void Main(string[] args)
      {
          string appendUrl = string.Format("api/values");
          string totalUrl = "http://localhost:51744/api/values";
          HttpClient client = new HttpClient();
          client.DefaultRequestHeaders.Add("Accept", "application/xml");
  
          string content = "Here is my input string";
          StringContent sContent = new StringContent(content, Encoding.UTF8, "application/xml");
  
          HttpResponseMessage response = null;
          string resultString = null;
  
          client.PostAsync(new Uri(totalUrl), sContent).ContinueWith(responseMessage =&gt;
              {
                  response = responseMessage.Result;
              }).Wait();
  
          response.Content.ReadAsStringAsync().ContinueWith(stream =&gt;
              {
                  resultString = stream.Result;
              }).Wait();          
      }
  </code></pre>
  
  <p>I'm new to the MVC web api and use of HttpClient - any help pointing me in the right direction would be greatly appreciated.</p>