[英]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. 我在MVC 4.5 WebApi项目中收到500内部服务器错误。 I can successfully call my webservice with a GET and a GET with an Id.
我可以通过GET和带有Id的GET成功调用我的web服务。 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. 我可以在
Application_BeginRequest()
设置一个断点,并确认我先收到一个OPTIONS请求,然后是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. 控制器中的方法没有被调用,我已经向Global.asax.cs添加了一个也没有被命中的
Application_Error()
方法。 The html page is doing a CORS but I have already handled that using ThinkTecture.IdentityModel . html页面正在执行CORS,但我已经使用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=?=
To see all exceptions turn following setting on: 要查看所有例外情况,请执行以下设置:
You may need to uncheck "Tools->Options->Debugging->My code only" option if exception is thrown really outside of your code. 如果异常抛出代码之外,您可能需要取消选中“工具 - >选项 - >调试 - >仅我的代码”选项。
You can try registering to the Error
event of the HttpApplication
object that's handling the request: 您可以尝试注册处理请求的
HttpApplication
对象的Error
事件:
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()
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.