title: Why is WebAPI Throwing a 'No MediaTypeFormatter' Error?
body: <p>I have a strange issue happening with my WebAPI controller.</p>
  
  <p>In my development environment (Win7, IIS 7.5) the method on the controller is called and the result is returned as expected.</p>
  
  <p>In my integration environment (WIn Server 2003, IIS 6.0) I have deployed the code by copying it from my development box to the integration box. The method on the controller is called and returns the following error:</p>
  
  <blockquote>
    <p>"No MediaTypeFormatter is available to read an object of type "EmailQueueResult"
    from content with media type "text/html"."</p>
  </blockquote>
  
  <p>What I don't understand is why I only get this error in my integration environment and not in my development environment.</p>
  
  <pre><code>    public HttpResponseMessage Post([FromBody]EmailQueueMessage email)
      {
          EmailQueueResult result;
  
          if (email != null)
          {
              try
              {
                  var emailQueueIdList = QueueEmails(email);
  
                  QueueAttachments(email, emailQueueIdList);
  
                  foreach(var emailQueueId in emailQueueIdList)
                  {
                      UpdateEmailQueueDateQueued(emailQueueId, DateTime.Now);
                  }
  
                  result = new EmailQueueResult(0, "Email added to queue.", "", null);
              }
              catch (Exception e) {
                  result = new EmailQueueResult(2, "Error occurred.", e.Message, null);
              }
          }
          else
          {
              result = new EmailQueueResult(1, "Email passed in was null.", "", null);
          }
  
          return ControllerContext.Request.CreateResponse(HttpStatusCode.OK, result, "application/json");
      }
  </code></pre>
  
  <p>WebApiConfig.cs:</p>
  
  <pre><code>namespace App_Start
  {
      public static class WebApiConfig
      {
          public static void Register(HttpConfiguration config)
          {
              config.Routes.MapHttpRoute(
                  "ControllerWithBooleanParameter",
                  "api/{controller}/{showSentEmails}",
                  null,
                  new { boolConstraint = new IsBoolean() }
              );
              config.Routes.MapHttpRoute(
                  "ControllerWithIntegerParameter",
                  "api/{controller}/{id}",
                  null,
                  new { id = @"^\d+$" }
              );
              config.Routes.MapHttpRoute(
                  "Default",
                  "api/{controller}/{id}",
                  new { id = UrlParameter.Optional }
              );
          }
      }
  }
  </code></pre>
  
  <p>Web.config:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;!--
    For more information on how to configure your ASP.NET application, please visit
    http://go.microsoft.com/fwlink/?LinkId=169433
    --&gt;
  &lt;configuration&gt;
      &lt;appSettings&gt;
          &lt;add key="webpages:Version" value="2.0.0.0" /&gt;
          &lt;add key="webpages:Enabled" value="false" /&gt;
          &lt;add key="PreserveLoginUrl" value="true" /&gt;
          &lt;add key="ClientValidationEnabled" value="true" /&gt;
          &lt;add key="UnobtrusiveJavaScriptEnabled" value="true" /&gt;
      &lt;/appSettings&gt;
      &lt;system.net&gt;
          &lt;settings&gt;
              &lt;httpWebRequest useUnsafeHeaderParsing="true" /&gt;
          &lt;/settings&gt;
          &lt;mailSettings&gt;
              &lt;smtp from="address@email.com"&gt;
                  &lt;network host="smtp.server.com" port="25" /&gt;
              &lt;/smtp&gt;
          &lt;/mailSettings&gt;
      &lt;/system.net&gt;
      &lt;system.web&gt;
          &lt;compilation debug="true" targetFramework="4.0" /&gt;
          &lt;authentication mode="None" /&gt;
          &lt;pages&gt;
              &lt;namespaces&gt;
                  &lt;add namespace="System.Web.Helpers" /&gt;
                  &lt;add namespace="System.Web.Mvc" /&gt;
                  &lt;add namespace="System.Web.Mvc.Ajax" /&gt;
                  &lt;add namespace="System.Web.Mvc.Html" /&gt;
                  &lt;add namespace="System.Web.Optimization" /&gt;
                  &lt;add namespace="System.Web.Routing" /&gt;
                  &lt;add namespace="System.Web.WebPages" /&gt;
              &lt;/namespaces&gt;
          &lt;/pages&gt;
      &lt;/system.web&gt;
      &lt;system.webServer&gt;
          &lt;validation validateIntegratedModeConfiguration="false" /&gt;
          &lt;modules runAllManagedModulesForAllRequests="true" /&gt;
          &lt;handlers&gt;
              &lt;remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" /&gt;
              &lt;remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" /&gt;
              &lt;remove name="ExtensionlessUrlHandler-Integrated-4.0" /&gt;
              &lt;add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" /&gt;
              &lt;add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" /&gt;
              &lt;add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /&gt;
          &lt;/handlers&gt;
      &lt;/system.webServer&gt;
      &lt;runtime&gt;
          &lt;assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"&gt;
              &lt;dependentAssembly&gt;
                  &lt;assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" /&gt;
                  &lt;bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" /&gt;
              &lt;/dependentAssembly&gt;
              &lt;dependentAssembly&gt;
                  &lt;assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /&gt;
                  &lt;bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" /&gt;
              &lt;/dependentAssembly&gt;
              &lt;dependentAssembly&gt;
                  &lt;assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" /&gt;
                  &lt;bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" /&gt;
              &lt;/dependentAssembly&gt;
          &lt;/assemblyBinding&gt;
      &lt;/runtime&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>I have also tried having this method just return a straight EmailQueueResult, and that gave me the same results.</p>
  
  <p>EDIT:
  Here are some more tests that I tried along with their results:</p>
  
  <ul>
  <li>When I send a POST request to the web service on my local machine using an external tool it works as expected.</li>
  <li>When I execute code that calls the web service using my local copy of the code it works as expected.</li>
  <li>When I send a POST request to the web service on the integration machine using an external tool it works as expected.</li>
  <li>When I execute code that calls the web service using the integration machine I get the above error.</li>
  </ul>
  
  <p>The line of code that sends the POST:</p>
  
  <pre><code>        EmailQueueResult result = PostToWebService&lt;EmailQueueMessage, EmailQueueResult&gt;(EmailQueueBaseAddress, "application/json", EmailQueueUrlToCall, emailQueueMessage);
  </code></pre>
  
  <p>The PostToWebService method that actually makes the call:</p>
  
  <pre><code>    protected returnType PostToWebService&lt;dataType, returnType&gt;(string baseAddress, string mediaType, string urlToCall, dataType data)
      {
          HttpClient client = new HttpClient();
          client.BaseAddress = new Uri(baseAddress);
  
          client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(mediaType));
  
          MediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter();
  
          HttpContent content = new ObjectContent&lt;dataType&gt;(data, jsonFormatter);
  
          HttpResponseMessage response = client.PostAsync(urlToCall, content).Result;
  
          returnType result = response.Content.ReadAsAsync&lt;returnType&gt;().Result;
  
          return result;
      }
  </code></pre>
  
  <p><strong>UPDATE</strong>: I installed the website and web service on a 2008 server. The only combination that fails seems to be when the web service on the 2003 box is called from the website on the 2003 box (2003 site -> 2003 service). The other three combinations (2008 site - > 2008 service, 2008 site -> 2003 service, 2003 site -> 2008 service) all seem to be working correctly.</p>