65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 Global.asax 和 Membership 模型自定义错误消息

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2013 年 10 月 11 日

CPOL

3分钟阅读

viewsIcon

8114

场景:使用 ASP.NET membership 模型向网站的普通用户显示自定义错误消息。将所有详细信息作为电子邮件发送给网站管理员。

场景:使用 ASP.NET membership 模型向网站的普通用户显示自定义错误消息。将所有详细信息作为电子邮件发送给网站管理员。此外,在开发环境中,只显示详细的异常信息,不显示自定义错误消息。

环境: VS 2005, ASP.NET 2.0

要求

  • 项目中应使用 ASP.NET membership 和身份验证模型。
  • 为了发送电子邮件,需要了解要使用的 smtp 地址。

按照以下步骤,可以实现上述场景。

步骤 1:在项目中添加一个自定义错误页面,并将其命名为 ErrorMessage.aspx。

步骤 2:为现有项目添加一个全局应用程序类,并将其命名为默认的 Global.asax。

步骤 3:在 Global.asax 页面的 Application_Error 子例程中,添加以下代码:

Vb.Net 代码

If (Context IsNot Nothing) And (Context.User.IsInRole("Admin")) Then '检查用户是否为管理员
            Dim err As Exception = Server.GetLastError() '从服务器获取最后一次错误
            Response.Clear()
            Response.Write("<h1>" & err.InnerException.Message & "</h1>") '显示错误标题和实际错误消息
            Response.Write("<pre>" & err.ToString & "</pre>")
            Server.ClearError()
        End If
If (Context IsNot Nothing) And (Context.User.IsInRole("Admin") = False) Then '如果用户不是管理员
            Dim err As Exception = Server.GetLastError()
            Dim msgMail As New MailMessage()
            msgMail.From = New MailAddress("abc@xyz.com") '指定发件人电子邮件地址
            msgMail.Subject = "Error"
            msgMail.To.Add(New MailAddress("wer@xyz.com")) '指定收件人电子邮件地址
            msgMail.To.Add(New MailAddress("wty@asd.com"))
            Dim errorurl As String = Request.Path
            Dim ErrorTitle As String = Err.InnerException.Message
            Dim ErrorMessage As String = Err.ToString()
            msgMail.IsBodyHtml = True
            Dim strBody As String = "<html><body>" + "发生了一个错误。用户请求了页面 " & errorurl &    "<br/>" & ErrorTitle & "<br/>" & ErrorMessage & ". " + " </body></html>"
            msgMail.Body = strBody
            Dim smtpMail As SmtpClient
            smtpMail = New SmtpClient("xyz.com") '指定 smtp 地址
            smtpMail.Send(msgMail) '发送电子邮件
            msgMail.Dispose()
            Server.ClearError()
            Server.Transfer("ErrorMesage.aspx") '将用户重定向到自定义错误页面
        End If

 

C#.Net 代码

 <%@ Import Namespace="System.Net.Mail"  %>

     // 发生未处理的错误时运行的代码
        HttpContext context;
        context = HttpContext.Current;
       
        Exception err = Server.GetLastError();
       
        //检查用户是否为管理员
        if ((Context != null) && (Context.User.IsInRole("Admin")))
        { 
            //从服务器获取最后一次错误
            Response.Clear();
            Response.Write("<h1>" + err.InnerException.Message + "</h1>");
            //显示错误标题和实际错误消息
            Response.Write("<pre>" + err.ToString() + "</pre>");
            Server.ClearError();
        }
        if ((Context != null) && (Context.User.IsInRole("Admin") == false))
        {
            MailMessage msgMail = new MailMessage();
            msgMail.From = new MailAddress("abc@xyz.com");
            //指定发件人电子邮件地址
            msgMail.Subject = "Error";
            msgMail.To.Add(new MailAddress("wer@xyz.com"));
            //指定收件人电子邮件地址
            msgMail.To.Add(new MailAddress("wty@asd.com"));
            string errorurl = Request.Path;
            string ErrorTitle = err.InnerException.Message;
            string ErrorMessage = err.ToString();
            msgMail.IsBodyHtml = true;
            string strBody = "<html><body>" + "发生了一个错误。用户请求了页面 " + errorurl + "<br/>" + ErrorTitle + "<br/>" + ErrorMessage + ". " + " </body></html>";
            msgMail.Body = strBody;
            SmtpClient smtpMail = default(SmtpClient);
            smtpMail = new SmtpClient("xyz.com");
            //指定 smtp 地址
            smtpMail.Send(msgMail);
            //发送电子邮件
            msgMail.Dispose();
            Server.ClearError();
            //将用户重定向到自定义错误页面
            Server.Transfer("ErrorMesage.aspx");
        }

 

 

添加代码并生成项目后,通过故意在项目中创建一个错误来测试。请确保项目中引起的错误是运行时错误。尝试测试管理员和非管理员的两种情况。

 

© . All rights reserved.