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

2024-01-01

我在 MVC 4.5 WebApi 项目中收到 500 内部服务器错误。我可以使用 GET 和带有 Id 的 GET 成功调用我的 Web 服务。但是,当我发布文件时,我收到错误。我可以设置一个断点Application_BeginRequest()并确认我首先收到 OPTIONS 请求,然后收到 POST。控制器中的方法没有被调用,我添加了一个Application_Error()Global.asax.cs 的方法也不会被命中。 html 页面正在执行 CORS,但我已经使用它进行了处理ThinkTcture.IdentityModel http://nuget.org/packages/Thinktecture.IdentityModel。我正在关注代码here https://stackoverflow.com/questions/5392344/sending-multipart-formdata-with-jquery-ajax用于文件上传。

有任何想法吗?

这是客户端代码:

    <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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 ajax 调用 WebApi 时如何调试 500 内部服务器错误? 的相关文章

随机推荐