Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I debug a 500 Internal Server Error when calling a WebApi from ajax?

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=?=
like image 277
Bruce C Avatar asked Nov 06 '12 21:11

Bruce C


People also ask

What is 500 Internal Server error Ajax?

The 500 Internal Server Error is a very general HTTP status code. It means something has gone wrong on the website and webserver is unable to specify what exactly, thus failing in fulfilling the request made by the client.

What if you get 500 status codes in the REST API response?

The HTTP status code 500 is a generic error response. It means that the server encountered an unexpected condition that prevented it from fulfilling the request. This error is usually returned by the server when no other error code is suitable.


1 Answers

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.

like image 90
Alexei Levenkov Avatar answered Oct 05 '22 05:10

Alexei Levenkov