我试图在我的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'对象。这只是内容? – Detilium
@BasantaMatia'HttpResponseMessage'给了我同样的问题。仍然我的响应类型和对象的类型是'Blob' – Detilium