【发布时间】:2017-08-12 19:16:14
【问题描述】:
我正在尝试在我的 MCV AngularJS 应用程序中实现一些错误处理,但遇到了一个我不确定如何解决的问题。
结构
在我的 AngularJS 服务 ticketService 我有以下方法:
this.downloadFile = function (fileId) {
return $http.get(baseUrl + "/Downloadfile/" + fileId, { responseType: "blob" });
}
在我的控制器中:
$scope.downloadFile = function (fileId) {
ticketService.downloadFile(fileId)
.then(function (response) {
// Handle correct request and response
}, function (err) {
// Handle error
notify({ message: "Something went wrong: " + err.data.Message, position: "center", duration: 10000 });
})
}
这是我从后端 MVC Web API 方法返回的内容:
var error = new HttpError("Failed to find file, bla bla bla.");
return Request.CreateResponse(HttpStatusCode.NotFound, error);
问题
我的问题是,由于我的responseType 设置为blob,我的err 对象是相同的响应类型。我相信我的后端服务应该可以覆盖此响应类型,并使用包含一些 Message 的对象进行响应。
从这个回复中,我以为我可以得到err.data.Message,但也许我误解了这个场景?
提前谢谢你。
【问题讨论】:
-
你检查过 API 方法中的“HttpResponseMessage”吗?
-
我不确定如何将自定义消息应用到
HttpResponseMessage对象。只是内容上的吗? -
@BasantaMatia
HttpResponseMessage给了我同样的问题。我的响应类型和对象仍然是Blob
标签: angularjs asp.net-web-api angularjs-http