2017-03-21 4 views
0

MCV AngularJS 응용 프로그램에 오류 처리를 구현하려고하지만이 문제를 해결할 방법이 확실하지 않습니다.콘텐츠 형식 응답이 포함 된 사용자 지정 오류 메시지

구조 내 AngularJS와에서
서비스 ticketService 내가 가지고있는 다음과 같은 방법 : 내가 백엔드 MVC 웹 API 메서드에서 반환 무엇

$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 }); 
     }) 
} 
다음

가있다 :

this.downloadFile = function (fileId) { 
    return $http.get(baseUrl + "/Downloadfile/" + fileId, { responseType: "blob" }); 
} 

그리고 내 컨트롤러에서

:

var error = new HttpError("Failed to find file, bla bla bla."); 
return Request.CreateResponse(HttpStatusCode.NotFound, error); 

문제
내 문제는 내 responseTypeblob로 설정되어 있기 때문에, 내 err 객체가 동일한 응답 유형 것입니다. 내 백엔드 서비스가이 응답 유형을 무시하고 일부 Message이 포함 된 객체로 응답 할 수 있어야한다고 믿습니다.

이 응답에서 나는 err.data.Message을 얻을 수있을 것이라고 생각했지만이 시나리오를 오해했을 것입니까?

미리 감사드립니다.

+0

API 메소드에서 "HttpResponseMessage"로 확인 했습니까? –

+0

'HttpResponseMessage' 객체에 커스텀 메시지를 어떻게 적용하는지 모르겠습니다. 그것은 단지 내용에 있습니까? – Detilium

+0

@BasantaMatia'HttpResponseMessage'는 나에게 같은 문제를 안겨다. 여전히 내 응답 유형 및 객체는 'Blob' 유형입니다 – Detilium

답변

0
public HttpResponseMessage Get(int id) 
    { 
     try 
     { 
      return Request.CreateResponse(HttpStatusCode.OK, new { Status = 
        "OK", Message = this._myContext.GetCustomer(id) }); 
     } 
     catch(Exception e) 
     { 
      return Request.CreateResponse(HttpStatusCode.Conflict, new { 
        Status = "NO", Message = e.ToString() }); 
     } 
    } 

"파일을 찾을 수 없습니다. bla bla bla"와 같은 메시지를 반환 할 수 있습니다. in 메시지. 그럼 그냥 data.Message,

+0

내 오류 콜백에 영향을 미치지 않습니까? – Detilium

0

은 $ HTTP 서비스가 비행에 responseType을 변경할 수하지 않은 XHR API를 사용하는 것처럼 아약스 성공 방법에 확인해야합니다.

당신은 상태 메시지를 설정하고 사용할 수 있습니다

$scope.downloadFile = function (fileId) { 
    return ticketService.downloadFile(fileId) 
     .then(function (response) { 
      // Handle correct request and response 
      return response.data; 
    }).catch(function (response) { 
      // Handle error 
      console.log(response.status); 
      console.log(response.statusText); 
      throw response; 
    }); 
}; 

대체 방법은 다음과 같습니다 :

  1. 를 사용하여 더 강력한을 가지고 Fetch API AngularJS와에 그런

    public ActionResult Foo() 
    { 
        Response.StatusCode = 403; 
        Response.StatusDescription = "Some custom message"; 
    
        return View(); // or Content(), Json(), etc 
    } 
    

    을 유연한 기능 세트.

  2. BlobJavaScript Object으로 변환하려면 FileReader APIJSONparse() method을 사용하십시오.