title: How can I debug a 500 Internal Server Error when calling a WebApi from ajax?
body: <p>I am receiving a 500 Internal Server Error in a MVC 4.5 WebApi project.  I can successfully call my webservice with a GET and a GET with an Id.  But, when I POST a file I am getting the error.  I can set a breakpoint in <code>Application_BeginRequest()</code> and confirm that I am receiving first an OPTIONS request and then the POST.  The method in the controller is not getting called and I have added an <code>Application_Error()</code> method to Global.asax.cs that does not get hit either.  The html page is doing a CORS but I have already handled that using <a href="http://nuget.org/packages/Thinktecture.IdentityModel" rel="nofollow noreferrer">ThinkTecture.IdentityModel</a>.  I am following the code <a href="https://stackoverflow.com/questions/5392344/sending-multipart-formdata-with-jquery-ajax">here</a> for the file upload.</p>
  
  <p>Any ideas?</p>
  
  <p>Here is the client code:</p>
  
  <pre><code>    &lt;script type="text/javascript"&gt;
          var UploadDocument = function () {
              var url = sessionStorage.getItem("url");
              var auth = sessionStorage.getItem("auth");
              var data = new FormData();
  
              jQuery.each($('#fileToUpload')[0].files, function (i, file) {
                  data.append('file-' + i, file);
              });
  
              jQuery.support.cors = true;
              $.ajax({
                  type: "POST",
                  url: url,
                  data: data,
                  cache: false,
                  processData: false,
                  contentType: false,
                  //dataType: "json",
                  headers: { Authorization: 'Basic ' + auth },
                  crossDomain: true,
                  success: function (data, textStatus, jqXHR) {
                      alert('Success');
                  },
                  error: function (jqXHR, textStatus, errorThrown) { alert('Error: ' + textStatus + ' ' + errorThrown.message); }
              });
          };
  
      &lt;/script&gt;
  </code></pre>
  
  <p>My controller code looks like this:</p>
  
  <pre><code>    public int Post(HttpPostedFileBase FileToUpload)
      {
          // Do stuff with the file
      }
  </code></pre>
  
  <p>The request and response look like this:</p>
  
  <pre><code>Request URL:http://localhost:51234/api/TaxDocument
  Request Method:POST
  Status Code:500 Internal Server Error
  
  Request Headers
  Accept:*/*
  Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
  Accept-Encoding:gzip,deflate,sdch
  Accept-Language:en-US,en;q=0.8
  Authorization:Basic YmNhbGxlbjpuZWxsYWM=
  Connection:keep-alive
  Content-Length:2054044
  Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryIGzPKhvRVwFXbupu
  Host:localhost:51234
  Origin:http://localhost:52386
  Referer:http://localhost:52386/Upload.html
  User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4
  
  Response Headers:
  Access-Control-Allow-Credentials:true
  Access-Control-Allow-Origin:http://localhost:52386
  Cache-Control:no-cache
  Content-Length:1133
  Content-Type:application/json; charset=utf-8
  Date:Tue, 06 Nov 2012 21:10:23 GMT
  Expires:-1
  Pragma:no-cache
  Server:Microsoft-IIS/8.0
  X-AspNet-Version:4.0.30319
  X-Powered-By:ASP.NET
  X-SourceFiles:=?UTF-8?B?QzpccHJvamVjdHNcU2F2ZU15VzJcU2F2ZU15VzIuV2Vic2VydmljZVxhcGlcVGF4RG9jdW1lbnQ=?=
  </code></pre>