0

Im new to C# ASP.net. I use Monodevelop in Ubuntu to create a simple WebForm.

Default.aspx.cs:

public partial class Default : System.Web.UI.Page
{
    protected void Page_Error(object sender, EventArgs e)
    {
            Exception Ex = Server.GetLastError();
            Console.WriteLine(Ex.Message);
    }


    [System.Web.Services.WebMethod]
    public static string TestUnhandledException()
    {
        throw new Exception("abc");
    }
}

Default.aspx

<%@ Page Language="C#" Inherits="MyFirstWebForm.Default" %>
<!DOCTYPE html>
<html>

<head runat="server">
    <title>Default</title>
    <script>
        function Test() {
            PageMethods.TestUnhandledException(OnSuccessCallback, OnFailureCallback);
        }

        function OnSuccessCallback(res) {}
        function OnFailureCallback() {}
    </script>

</head>

<body>
    <form id='form1' runat='server'>
        <asp:scriptmanager enablepagemethods="true" id="scpt" runat="server"> </asp:scriptmanager>
        <asp:button id="Button1" runat="server" text="Initialize" OnClientClick='Test();return false;'/>
    </form>

</body>
</html>

The method TestUnhandledException() works fine, but as I debug, there is no exception message show and Page_Error() never runs.

Do I miss something ? How should I log the unhandled exception in ASP.NET webform ? I appreciate any help.

Update: The exception throw by Page_Load is caught by Page_Error but the one thrown by [WebMethod] does not.

2 Answers 2

1

It seems it is not simple to catch unhandled exception thrown by WebMethod. From this post:

you can't just add a few lines of code to the Global.aspx.cs file in the Application_Error event handler method to log unhandled exceptions because the ASP.NET Web Services pipeline bypasses this event so it can return all the exceptions back to the caller.

So probably the most simple way is to surround the method with try-catch block as shown in the post

Sign up to request clarification or add additional context in comments.

Comments

0

Did you check inner Exception ?. your exception message should be in there

1 Comment

The problem is as I debug, Page_Error() never runs in the first place

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.