繁体   English   中英

从ajax调用WebApi时如何调试500内部服务器错误?

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

我在MVC 4.5 WebApi项目中收到500内部服务器错误。 我可以通过GET和带有Id​​的GET成功调用我的web服务。 但是,当我发布文件时,我收到错误。 我可以在Application_BeginRequest()设置一个断点,并确认我先收到一个OPTIONS请求,然后是POST。 控制器中的方法没有被调用,我已经向Global.asax.cs添加了一个也没有被命中的Application_Error()方法。 html页面正在执行CORS,但我已经使用ThinkTecture.IdentityModel处理了它。 我在这里关注文件上传的代码。

有任何想法吗?

这是客户端代码:

    <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>

我的控制器代码如下所示:

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

请求和响应如下所示:

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=?=

要查看所有例外情况,请执行以下设置:

  • VS 2013(及以下):调试 - >例外 - > CLR - 检查“何时抛出”。
  • VS 2015:调试 - > Windows - >异常设置 - > CLR

如果异常抛出代码之外,您可能需要取消选中“工具 - >选项 - >调试 - >仅我的代码”选项。

您可以尝试注册处理请求的HttpApplication对象的Error事件:

HttpApplicationReference.Error += ErrorHandler;

处理错误的方法:

public void ErrorHandler(object sender, EventArgs e)
{
    System.Diagnostics.Debug.Write(
        ((System.Web.HttpApplication)sender).Server.GetLastError().ToString()
    );
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM