如何更好地调试WebService asmx - 500内部服务器错误

时间:2011-08-15 19:25:44

标签: jquery web-services json

我需要将我的Web服务中的错误以Json格式直接发送到客户端,以便我可以准确读取正在抛出的错误。我该怎么办?

这就是我一直在Fiddler中获得的

Runtime Error 
Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed. 

Details: To enable the details of this specific error message to be viewable on the local server machine, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "RemoteOnly". To enable the details to be viewable on remote machines, please set "mode" to "Off".


<!-- Web.Config Configuration File -->

<configuration>
    <system.web>
        <customErrors mode="RemoteOnly"/>
    </system.web>
</configuration>

服务器代码

  <WebMethod(EnableSession:=True)> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
 Protected Function PopulateUsers() As IDictionary(Of String, String)

        Dim users As New Dictionary(Of String, String)

        Try
            Dim arry As ArrayList =  UserController.GetUsers(0, True)
            For Each objUser As Users In arry 
                userrole.Add(CStr(objUser.UserID), objUser)
            Next
        Catch ex As Exception
            Dim ur As New Dictionary(Of String, String)
            Dim s As New User
            s.DisplayName = ex.GetBaseException.StackTrace
            s.UserID = 1
            users.Add(ex.GetBaseException.Message, s)
            Return s
        End Try

        Return users 
    End Function

2 个答案:

答案 0 :(得分:0)

确保您的网络服务中有正确的错误处理。如果有任何错误,请将您的错误对象转换为格式良好的json对象并发送它。在客户端,只会调用成功处理程序,但您需要检查响应是否包含错误对象或实际响应,并采取相应的行动。

你的catch块有错误,试试这个

Protected Function PopulateUsers() As IDictionary(Of String, String)

        Dim users As New Dictionary(Of String, String)

        Try
            Dim arry As ArrayList =  UserController.GetUsers(0, True)
            For Each objUser As Users In arry 
                userrole.Add(CStr(objUser.UserID), objUser)
            Next
        Catch ex As Exception
            Dim ur As New Dictionary(Of String, String)

            ur.Add("ERROR", ex.GetBaseException.StackTrace)
            Return ur
        End Try

        Return users 
    End Function

答案 1 :(得分:0)

使用elmah获取包含报告和警报的完整错误记录。

相关问题