在REST中没有收到自定义错误消息

时间:2016-03-03 07:07:27

标签: asp.net vb.net wcf rest

我正在开发REST API。 以下是我的代码:

在IService.vb

response = get_response() # get response is a string containing the page.
tokens = response.split("<!doctype html>") # won't work well.
return ''.join(tokens)

在Service.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)

在Web.Config文件中

    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

我正在

请求错误:服务器在处理请求时遇到错误。有关详细信息,请参阅服务器日志。

当我在客户端ID中传递String值时。虽然我已经实现了FaultExpcetion,但我从未在POSTMAN或Web客户端中收到“无效的客户端ID”消息。

任何人都可以帮助我如何在POSTMAN或Web客户端上获取自定义消息?

1 个答案:

答案 0 :(得分: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

因此,这将为我的自定义错误消息生成Webexception,我想在我的客户端应用程序上显示: 现在在我的客户端应用程序中使用

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

希望这对某人有帮助。