16

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 Application_BeginRequest() 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 Application_Error() 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 ThinkTecture.IdentityModel. I am following the code here for the file upload.

Any ideas?

Here is the client code:

    <script type="text/javascript">
        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); }
            });
        };

    </script>

My controller code looks like this:

    public int Post(HttpPostedFileBase FileToUpload)
    {
        // Do stuff with the file
    }

The request and response look like this:

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=?=
Community
  • 1
  • 1
Bruce C
  • 349
  • 1
  • 6
  • 17

2 Answers2

40

To see all exceptions turn following setting on:

  • VS 2013 (and below): Debug -> Exceptions -> CLR - check "when thrown".
  • VS 2015: Debug -> Windows -> Exception Settings -> CLR

You may need to uncheck "Tools->Options->Debugging->My code only" option if exception is thrown really outside of your code.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • 9
    Thank you so much. It was the Tools->Options->Debugging->General->Enable Just My Code was checked. I already enabled breaking on any exception. Now I can see the real problem "Multiple actions were found that match the request:" – Bruce C Nov 06 '12 at 22:01
  • 1
    VS15 - Debug > Windows > Exception Settings > CLR – dispake May 05 '16 at 19:58
  • 2
    Be careful to UNDO any tinkering here in Exception settings.. while this worked perfectly for me (allowed me to send requests from my WinForms' HttpClient project over to my WebApi project, and debug any 500 errors) - it worked because I changed the settings *while* the WebApi project was running. **However**, after I closed the WebApi project, the next time I went to start it up, it kept crashing due to UnauthorizedAccessExceptions.. so, weirdly enough, my WebApi project would not relaunch until I UNchecked the CLR exceptions in my VS 2015 http://stackoverflow.com/a/36301485/1520850 – bkwdesign May 09 '16 at 15:43
0

You can try registering to the Error event of the HttpApplication object that's handling the request:

HttpApplicationReference.Error += ErrorHandler;

The method that handles the error:

public void ErrorHandler(object sender, EventArgs e)
{
    System.Diagnostics.Debug.Write(
        ((System.Web.HttpApplication)sender).Server.GetLastError().ToString()
    );
}
Protector one
  • 6,926
  • 5
  • 62
  • 86