【发布时间】:2016-03-14 04:20:36
【问题描述】:
我是 Web API 的新手(在 VS 2012 中使用 C#)并试图帮助一个项目,该项目涉及将更有意义的错误返回到 AngularJS 前端,但我不确定是否有关于以下方面的首选方法只是捕捉到错误并说
return Request.CreateErrorResponse(..
对比
throw new HttpResponseException(Request.CreateErrorResponse(...
我不完全了解返回响应错误的优缺点或用途。
关于更多背景知识,我们正在上传 Excel 文件,并且我们有各种在解析等时会导致错误的用例。我想异步返回错误消息,以明确导致错误的原因,而不是模棱两可的“异常”。
所以我正在尝试诸如
try
{
//stuff
}
catch (FormatException e)
{
return Request.CreateErrorResponse(HttpStatusCode.Conflict, "issue 1");
}
catch (FileFormatException e)
{
return Request.CreateErrorResponse(HttpStatusCode.Conflict, "issue 2");
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.Conflict, "issue 3");
}
我可以给错误赋予更多的意义。我只是对是否应该“返回”Request.CreateErrorResponse 或“抛出”像 throw new HttpResponseException(Request.CreateErrorResponse(... 这样的异常感到困惑
在前端我只是读回那个错误
.error(function (data) {
$scope.uploadFailed = true; //shows the message span
$scope.uploadMsg = data["Message"].trim();
});
如果这令人困惑,请原谅我,我只是想确定哪种返回错误的方式最适合这种情况以及原因。
【问题讨论】:
标签: angularjs exception-handling httpresponse asp.net-web-api