<%@ Import Namespace="System.Web.Mail" %>
<html>
<script language="C#" runat="server">
void Error_500(Object sender, EventArgs e) {
String foo = null;
Response.Write(foo.ToString());
}
void Page_Error(Object sender, EventArgs e) {
String message = "<font face=verdana color=red>"
+ "<h4>" + Request.Url.ToString() + "</h4>"
+ "<pre><font color='red'>" + Server.GetLastError().ToString() + "</pre>"
+ "</font>";
Response.Write(message);
Response.Write("此服务器上发生了错误,已通知该站点的管理员。");
MailMessage mail = new MailMessage();
mail.From = "hollo08@hotmail.com";
mail.To = "hollo08@126.com";
mail.Subject = "站点错误";
mail.Body = message;
mail.BodyFormat = MailFormat.Html;
SmtpMail.Send(mail);
Server.ClearError();
}
</script>
<body>
<form runat="server">
<h4><font face="宋体">导致发生错误...</font></h4>
<asp:button text="500 服务器错误" OnClick="Error_500" width="150" runat="server"/><p>
</form>
</body>
</html>
博客展示了一段C#代码,用于处理服务器500错误。当错误发生时,会输出错误信息,包括请求的URL和错误详情,同时将错误信息通过邮件发送给管理员,最后清除错误。代码中还包含一个按钮,点击可触发500错误。
1万+

被折叠的 条评论
为什么被折叠?



