16

我在 MVC 4.5 WebApi 项目中收到 500 内部服务器错误。我可以使用 GET 和带有 Id 的 GET 成功调用我的 web 服务。但是,当我发布一个文件时,我收到了错误。我可以设置一个断点Application_BeginRequest()并确认我首先收到一个 OPTIONS 请求,然后是 POST。控制器中的方法没有被调用,我已经Application_Error()向 Global.asax.cs 添加了一个也没有被命中的方法。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=?=
4

2 回答 2

39

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.

于 2012-11-06T21:35:50.543 回答
0

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

HttpApplicationReference.Error += ErrorHandler;

处理错误的方法:

public void ErrorHandler(object sender, EventArgs e)
{
    System.Diagnostics.Debug.Write(
        ((System.Web.HttpApplication)sender).Server.GetLastError().ToString()
    );
}
于 2016-07-25T09:15:47.873 回答