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.