【问题标题】:Not getting Custom Error Message in REST在 REST 中未收到自定义错误消息
【发布时间】:2016-06-16 09:00:52
【问题描述】:

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

在 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"/>

我得到了

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

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

谁能帮助我了解如何在 POSTMAN 或 Web 客户端上获取我的自定义消息?

【问题讨论】:

    标签: asp.net vb.net wcf rest


    【解决方案1】:

    我可以通过以下实现来解决这个问题:

    我生成了一个自定义错误类:

    <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
    

    希望这对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2014-07-14
      • 2022-11-19
      • 2012-05-13
      • 1970-01-01
      • 2018-03-02
      • 1970-01-01
      • 2017-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多