2016-03-03 77 views
1

我正在開發REST API。 下面是我的代碼:未在REST中收到自定義錯誤消息

在IService.vb

<OperationContract(), 
     WebGet(UriTemplate:="/DCU/{ClientID}", 
       RequestFormat:=WebMessageFormat.Xml, 
       ResponseFormat:=WebMessageFormat.Xml, 
       BodyStyle:=WebMessageBodyStyle.Bare)> 
    Function GetAllData(ByVal ClientID As String) As List(Of Data) 

在Service.vb

Friend Class Service 
     Implements IService 

    Public Function GetAllData(ByVal intClientID As String) As System.Collections.Generic.List(Of Data) Implements ILightingGaleService.GetAllDCUs 
      Dim lstdata As New List(Of Data)()   
      If IsNumeric(intClientID) Then 
      Else 
       Throw New FaultException("Invalid Client ID") 
      End If 
    End Class 

在Web.Config文件

<serviceDebug includeExceptionDetailInFaults="true"/> 

我越來越

要求est錯誤:服務器在處理請求時遇到錯誤。查看服務器日誌獲取更多詳細信

當我在客戶端ID傳遞一個字符串值。儘管我已經實現了FaultExpcetion,但我從來沒有在POSTMAN或Web Client中收到「無效的客戶端ID」消息。

任何人都可以幫助我如何在POSTMAN或Web客戶端上獲得我的自定義消息嗎?

回答

0

我能夠通過下面落實解決此問題:

我生成一個類CustomError:

<DataContract> _ 
Public Class CustomError 
    Public Sub New(strError As String, strErrorDesc As String, strErrorCode As Long) 
     ErrorInfo = strError 
     ErrorDetails = strErrorDesc 
     ErroCode = strErrorCode 
    End Sub 

    <DataMember> _ 
    Public Property ErrorInfo() As String 
     Get 
      Return m_ErrorInfo 
     End Get 
     Private Set(value As String) 
      m_ErrorInfo = value 
     End Set 
    End Property 
    Private m_ErrorInfo As String 

    <DataMember> _ 
    Public Property ErrorDetails() As String 
     Get 
      Return m_ErrorDetails 
     End Get 
     Private Set(value As String) 
      m_ErrorDetails = value 
     End Set 
    End Property 
    Private m_ErrorDetails As String 

    <DataMember> _ 
    Public Property ErroCode() As Long 
     Get 
      Return m_ErroCode 
     End Get 
     Private Set(value As Long) 
      m_ErroCode = value 
     End Set 
    End Property 
    Private m_ErroCode As Long 
End Class 

在Service.vb

朋友服務等級 器具IService

Public Function GetAllData(ByVal intClientID As String) As System.Collections.Generic.List(Of Data) Implements ILightingGaleService.GetAllDCUs 
     Dim lstdata As New List(Of Data)()   
     If IsNumeric(intClientID) Then 
     Else 
      Dim ExceptionError As New CustomError("Client ID not found.", "The Client ID is not found in system.", HttpStatusCode.NotFound) 
      Throw New WebFaultException(Of CustomError)(ExceptionError, HttpStatusCode.NotFound) 
     End If 
End Class 

所以這會產生一個Webexcept離子爲此我想顯示在我的客戶端應用程序我的自定義錯誤消息: 現在我的客戶端應用程序我用下面

Catch ex As WebException 
      Dim strError = New StreamReader(ex.Response.GetResponseStream()).ReadToEnd() 
      lblErrorMsg.Text = strError 
End Try 

希望這有助於給別人。