【发布时间】:2017-03-25 11:02:21
【问题描述】:
我是 C# ASP.net 的新手。我在 Ubuntu 中使用 Monodevelop 创建一个简单的 WebForm。
默认.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");
}
}
默认.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>
TestUnhandledException() 方法工作正常,但在我调试时,没有显示异常消息并且 Page_Error() 永远不会运行。
我错过了什么吗?我应该如何在 ASP.NET webform 中记录未处理的异常?感谢您的帮助。
更新:Page_Load 抛出的异常被 Page_Error 捕获,但 [WebMethod] 抛出的异常没有。
【问题讨论】:
标签: c# asp.net exception-handling webforms