ASP.Net/Development/HttpException — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 11:51, 26 мая 2010

Convert Http Exception to String

<%@ Page Language="vb" EnableSessionState="false" %>
<html>
<head>
   <script runat="server">
      Sub Page_Load()
         Try
            Session("foo") = "Foo"
         Catch HttpEx As HttpException
            Message.Text = "ERROR:</br>"
            Message.Text &= "String Representation of Exception: " & _
               HttpEx.ToString() & "</br>"
         End Try
      End Sub
   </script>
</head>
<body>
   <asp:label id="Message" forecolor="red" runat="server"/>
</body>
</html>



Create a HttpException with error code

<%@ Page Language="vb" %>
<html>
<head>
   <script runat="server">
      Sub Page_Load()
         Try
            Throw New HttpException("Threw an error from Page_Load", 100)
         Catch HttpEx As HttpException
            Message.Text = "ERROR:</br>"
            Message.Text &= "Message: " & HttpEx.Message & "</br>"
            Message.Text &= "Error Code: " & HttpEx.ErrorCode & "</br>"
         End Try
      End Sub
   </script>
</head>
<body>
   <asp:label id="Message" forecolor="red" runat="server"/>
</body>
</html>



Get an Innter Exception Out

<%@ Page Language="vb" %>
<html>
<head>
   <script runat="server">
      Sub Page_Load()
         Try
            Dim myHttpEx As _
               New HttpException("This is a nested exception")
            Throw New HttpException("Threw an exception from Page_Load", _
               myHttpEx)
         Catch HttpEx As HttpException
            Dim InnerHttpEx As HttpException
            InnerHttpEx = HttpEx.InnerException
            Message.Text = "ERROR:</br>"
            Message.Text &= "Message: " & HttpEx.Message & "</br>"
            Message.Text &= "Inner Exception Message: " & _
               InnerHttpEx.Message & "</br>"
         End Try
      End Sub
   </script>
</head>
<body>
   <asp:label id="Message" forecolor="red" runat="server"/>
</body>
</html>



Get Base Exception

<%@ Page Language="vb" %>
<html>
<head>
   <script runat="server">
      Sub Page_Load()
         Try
            Dim myHttpEx As _
               New HttpException("This is the original exception")
            Dim myHttpEx2 As _
               New HttpException("This is a nested exception", myHttpEx)
            Throw New HttpException("Threw an exception from Page_Load", myHttpEx2)
         Catch HttpEx As HttpException
            Dim InnerHttpEx As HttpException
            InnerHttpEx = HttpEx.InnerException
            Message.Text = "ERROR:</br>"
            Message.Text &= "Message: " & HttpEx.Message & "</br>"
            Message.Text &= "Inner Exception Message: " & _
               InnerHttpEx.Message & "</br>"
            Message.Text &= "Base Exception Message: " & _
               InnerHttpEx.GetBaseException.Message & "</br>"
         End Try
      End Sub
   </script>
</head>
<body>
   <asp:label id="Message" forecolor="red" runat="server"/>
</body>
</html>



Get Html Error Message()

<%@ Page Language="vb" EnableSessionState="false" %>
<html>
<head>
   <script runat="server">
      Sub Page_Load()
         Try
            Server.Execute("DoesNotExist.aspx")
         Catch HttpEx As HttpException
            Message.Text = "ERROR:</br>"
            Message.Text &= "Http Error Message: " & _
               HttpEx.GetHtmlErrorMessage() & "</br>"
         End Try
      End Sub
   </script>
</head>
<body>
   <asp:label id="Message" forecolor="red" runat="server"/>
</body>
</html>



Get Http Code from HttpException

<%@ Page Language="vb" %>
<html>
<head>
   <script runat="server">
      Sub Page_Load()
         Try
            Server.Execute("Foo.aspx")
         Catch HttpEx As HttpException
            Message.Text = "ERROR:</br>"
            Message.Text &= "Http Status Code: " & _
               HttpEx.GetHttpCode() & "</br>"
         End Try
      End Sub
   </script>
</head>
<body>
   <asp:label id="Message" forecolor="red" runat="server"/>
</body>
</html>



HttpException: ErrorCode

<%@ Page Language="vb" EnableSessionState="false" %>
<html>
<head>
   <script runat="server">
      Sub Page_Load()
         Try
            Session("foo") = "Foo"
         Catch HttpEx As HttpException
            Message.Text = "ERROR:</br>"
            Message.Text &= "Message: " & HttpEx.Message & "</br>"
            Message.Text &= "Help Link: " & HttpEx.HelpLink & "</br>"
            Message.Text &= "Error Code: " & HttpEx.ErrorCode & "</br>"
            Message.Text &= "Source: " & HttpEx.Source & "</br>"
            Message.Text &= "Stack Trace: " & HttpEx.StackTrace & "</br>"
            Message.Text &= "Target Site: " & HttpEx.TargetSite.ToString() & "</br>"
            Message.Text &= "Http Error Message: " & HttpEx.GetHtmlErrorMessage() & "</br>"
            Message.Text &= "Http Error Code: " & HttpEx.GetHttpCode() & "</br>"
            Message.Text &= "String: " & HttpEx.ToString() & "</br>"
         End Try
      End Sub
   </script>
</head>
<body>
   <asp:label id="Message" forecolor="red" runat="server"/>
</body>
</html>



HttpException: GetHtmlErrorMessage()

<%@ Page Language="vb" EnableSessionState="false" %>
<html>
<head>
   <script runat="server">
      Sub Page_Load()
         Try
            Session("foo") = "Foo"
         Catch HttpEx As HttpException
            Message.Text = "ERROR:</br>"
            Message.Text &= "Message: " & HttpEx.Message & "</br>"
            Message.Text &= "Help Link: " & HttpEx.HelpLink & "</br>"
            Message.Text &= "Error Code: " & HttpEx.ErrorCode & "</br>"
            Message.Text &= "Source: " & HttpEx.Source & "</br>"
            Message.Text &= "Stack Trace: " & HttpEx.StackTrace & "</br>"
            Message.Text &= "Target Site: " & HttpEx.TargetSite.ToString() & "</br>"
            Message.Text &= "Http Error Message: " & HttpEx.GetHtmlErrorMessage() & "</br>"
            Message.Text &= "Http Error Code: " & HttpEx.GetHttpCode() & "</br>"
            Message.Text &= "String: " & HttpEx.ToString() & "</br>"
         End Try
      End Sub
   </script>
</head>
<body>
   <asp:label id="Message" forecolor="red" runat="server"/>
</body>
</html>



HttpException: GetHttpCode()

<%@ Page Language="vb" EnableSessionState="false" %>
<html>
<head>
   <script runat="server">
      Sub Page_Load()
         Try
            Session("foo") = "Foo"
         Catch HttpEx As HttpException
            Message.Text = "ERROR:</br>"
            Message.Text &= "Message: " & HttpEx.Message & "</br>"
            Message.Text &= "Help Link: " & HttpEx.HelpLink & "</br>"
            Message.Text &= "Error Code: " & HttpEx.ErrorCode & "</br>"
            Message.Text &= "Source: " & HttpEx.Source & "</br>"
            Message.Text &= "Stack Trace: " & HttpEx.StackTrace & "</br>"
            Message.Text &= "Target Site: " & HttpEx.TargetSite.ToString() & "</br>"
            Message.Text &= "Http Error Message: " & HttpEx.GetHtmlErrorMessage() & "</br>"
            Message.Text &= "Http Error Code: " & HttpEx.GetHttpCode() & "</br>"
            Message.Text &= "String: " & HttpEx.ToString() & "</br>"
         End Try
      End Sub
   </script>
</head>
<body>
   <asp:label id="Message" forecolor="red" runat="server"/>
</body>
</html>



HttpException: HelpLink

<%@ Page Language="vb" EnableSessionState="false" %>
<html>
<head>
   <script runat="server">
      Sub Page_Load()
         Try
            Session("foo") = "Foo"
         Catch HttpEx As HttpException
            Message.Text = "ERROR:</br>"
            Message.Text &= "Message: " & HttpEx.Message & "</br>"
            Message.Text &= "Help Link: " & HttpEx.HelpLink & "</br>"
            Message.Text &= "Error Code: " & HttpEx.ErrorCode & "</br>"
            Message.Text &= "Source: " & HttpEx.Source & "</br>"
            Message.Text &= "Stack Trace: " & HttpEx.StackTrace & "</br>"
            Message.Text &= "Target Site: " & HttpEx.TargetSite.ToString() & "</br>"
            Message.Text &= "Http Error Message: " & HttpEx.GetHtmlErrorMessage() & "</br>"
            Message.Text &= "Http Error Code: " & HttpEx.GetHttpCode() & "</br>"
            Message.Text &= "String: " & HttpEx.ToString() & "</br>"
         End Try
      End Sub
   </script>
</head>
<body>
   <asp:label id="Message" forecolor="red" runat="server"/>
</body>
</html>



HttpException: Message

<%@ Page Language="vb" EnableSessionState="false" %>
<html>
<head>
   <script runat="server">
      Sub Page_Load()
         Try
            Session("foo") = "Foo"
         Catch HttpEx As HttpException
            Message.Text = "ERROR:</br>"
            Message.Text &= "Message: " & HttpEx.Message & "</br>"
            Message.Text &= "Help Link: " & HttpEx.HelpLink & "</br>"
            Message.Text &= "Error Code: " & HttpEx.ErrorCode & "</br>"
            Message.Text &= "Source: " & HttpEx.Source & "</br>"
            Message.Text &= "Stack Trace: " & HttpEx.StackTrace & "</br>"
            Message.Text &= "Target Site: " & HttpEx.TargetSite.ToString() & "</br>"
            Message.Text &= "Http Error Message: " & HttpEx.GetHtmlErrorMessage() & "</br>"
            Message.Text &= "Http Error Code: " & HttpEx.GetHttpCode() & "</br>"
            Message.Text &= "String: " & HttpEx.ToString() & "</br>"
         End Try
      End Sub
   </script>
</head>
<body>
   <asp:label id="Message" forecolor="red" runat="server"/>
</body>
</html>



Http Exception Source

<%@ Page Language="vb" EnableSessionState="false" %>
<html>
<head>
   <script runat="server">
      Sub Page_Load()
         Try
            Session("foo") = "Foo"
         Catch HttpEx As HttpException
            Message.Text = "ERROR:</br>"
            Message.Text &= "Message: " & HttpEx.Message & "</br>"
            Message.Text &= "Source: " & HttpEx.Source & "</br>"
         End Try
      End Sub
   </script>
</head>
<body>
   <asp:label id="Message" forecolor="red" runat="server"/>
</body>
</html>



Http Exception Stace Trace

<%@ Page Language="vb" %>
<html>
<head>
   <script runat="server">
      Sub Page_Load()
         Try
            ThrowMeAnException
         Catch HttpEx As HttpException
            Message.Text = "ERROR:</br>"
            Message.Text &= "Message: " & HttpEx.Message & "</br>"
            Message.Text &= "Stack Trace: " & HttpEx.StackTrace & "</br>"
         End Try
      End Sub
      Sub ThrowMeAnException()
         Throw New HttpException("Threw an error from ThrowMeAnException")
      End Sub
   </script>
</head>
<body>
   <asp:label id="Message" forecolor="red" runat="server"/>
</body>
</html>



HttpException: StackTrace

<%@ Page Language="vb" EnableSessionState="false" %>
<html>
<head>
   <script runat="server">
      Sub Page_Load()
         Try
            Session("foo") = "Foo"
         Catch HttpEx As HttpException
            Message.Text = "ERROR:</br>"
            Message.Text &= "Message: " & HttpEx.Message & "</br>"
            Message.Text &= "Help Link: " & HttpEx.HelpLink & "</br>"
            Message.Text &= "Error Code: " & HttpEx.ErrorCode & "</br>"
            Message.Text &= "Source: " & HttpEx.Source & "</br>"
            Message.Text &= "Stack Trace: " & HttpEx.StackTrace & "</br>"
            Message.Text &= "Target Site: " & HttpEx.TargetSite.ToString() & "</br>"
            Message.Text &= "Http Error Message: " & HttpEx.GetHtmlErrorMessage() & "</br>"
            Message.Text &= "Http Error Code: " & HttpEx.GetHttpCode() & "</br>"
            Message.Text &= "String: " & HttpEx.ToString() & "</br>"
         End Try
      End Sub
   </script>
</head>
<body>
   <asp:label id="Message" forecolor="red" runat="server"/>
</body>
</html>



Http Exception Target Site

<%@ Page Language="vb" EnableSessionState="false" %>
<html>
<head>
   <script runat="server">
      Sub Page_Load()
         Try
            Session("foo") = "Foo"
         Catch HttpEx As HttpException
            Message.Text = "ERROR:</br>"
            Message.Text &= "Message: " & HttpEx.Message & "</br>"
            Message.Text &= "Target Site: " & HttpEx.TargetSite.Name & "</br>"
         End Try
      End Sub
   </script>
</head>
<body>
   <asp:label id="Message" forecolor="red" runat="server"/>
</body>
</html>



Indicate the Help link for Http Exception

<%@ Page Language="vb" %>
<html>
<head>
   <script runat="server">
      Sub Page_Load()
         Try
            Dim myHttpEx As _
               New HttpException("Threw an exception from Page_Load")
            myHttpEx.HelpLink = "http://www.nfex.ru"
            Throw myHttpEx
         Catch HttpEx As HttpException
            Message.Text = "ERROR:</br>"
            Message.Text &= "Message: " & HttpEx.Message & "</br>"
            Message.Text &= "Help Link: " & HttpEx.HelpLink & "</br>"
         End Try
      End Sub
   </script>
</head>
<body>
   <asp:label id="Message" forecolor="red" runat="server"/>
</body>
</html>



Inner Exception

<%@ Page Language="vb" %>
<html>
<head>
   <script runat="server">
      Sub Page_Load()
         Try
            Dim myHttpEx As _
               New HttpException("This is the original exception")
            Dim myHttpEx2 As _
               New HttpException("This is a nested exception", myHttpEx)
            Throw New HttpException("Threw an exception from Page_Load", myHttpEx2)
         Catch HttpEx As HttpException
            Dim InnerHttpEx As HttpException
            InnerHttpEx = HttpEx.InnerException
            Message.Text = "ERROR:</br>"
            Message.Text &= "Message: " & HttpEx.Message & "</br>"
            Message.Text &= "Inner Exception Message: " & _
               InnerHttpEx.Message & "</br>"
            Message.Text &= "Base Exception Message: " & _
               InnerHttpEx.GetBaseException.Message & "</br>"
         End Try
      End Sub
   </script>
</head>
<body>
   <asp:label id="Message" forecolor="red" runat="server"/>
</body>
</html>



Set Http Exception Message

<%@ Page Language="vb" %>
<html>
<head>
   <script runat="server">
      Sub Page_Load()
         Try
            Throw New HttpException("Threw an error from Page_Load")
         Catch HttpEx As HttpException
            Message.Text = "ERROR:</br>"
            Message.Text &= "Message: " & HttpEx.Message & "</br>"
         End Try
      End Sub
   </script>
</head>
<body>
   <asp:label id="Message" forecolor="red" runat="server"/>
</body>
</html>