ASP.NET分頁錯誤處理及郵件發送簡易方案

來源:互聯網
上載者:User

1包含頁面:Default.aspx,Error.aspx

2.思路:Global.asax頁面負責捕捉系統中除去try以外發生的分頁錯誤。並將錯資訊發送給Error.aspx頁面。Error.aspx頁面負責顯示錯誤資訊,並將錯誤資訊發送到指定郵箱。

3.具體代碼:

Default.aspx頁面

 

Code
html部分:
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    <asp:DropDownList ID="DropDownList1" runat="server" DataTextField="Name" 
        DataValueField="id">
    </asp:DropDownList>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    </form>
</body>
cs部分:
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("id",typeof(string)));
            dt.Columns.Add(new DataColumn("name", typeof(string)));

            dt.Rows.Add(dt.NewRow());
            dt.Rows[0][0] = "1";
            dt.Rows[0][1] = "1";
            this.DropDownList1.DataSource = dt;
            this.DropDownList1.DataBind();

        }
    }
 protected void Button1_Click(object sender, EventArgs e)
    {
        this.DropDownList1.SelectedValue = "fff";
    }

 

Global.asax代碼:

Code
<%@ Import Namespace ="System.Web" %>
 void Application_Error(object sender, EventArgs e) 
    {
        Exception  LastError = Server.GetLastError();
        if (LastError != null)
            Response.Redirect("error.aspx?error="+LastError.InnerException.ToString().Replace("\r\n",""));
    }

 

Error.aspx代碼:

 

Code
html部分:
<body>
    <form id="form1" runat="server">
    <div style="background-color: #99CCFF; height: 252px;">
        抱歉:發生了錯誤。
     <div style="background-color:Silver"><asp:Label ID="Label1" runat="server" 
            Text="Label"></asp:Label></div>
    
    </div>
    </form>
</body>
cs部分:

添加命名空間:

using System.Net.Mail;
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request["error"] != null && Request["error"].Length > 0)
            {
                this.Label1.Text = Request["error"];
                SendMail(Request["error"]);
            }
        }
    }
 public void SendMail(string body)
    {
        MailMessage myMail = new MailMessage();
        
        myMail.From = new MailAddress("myaccount@test.com");
        myMail.To.Add("test@test.com");
        myMail.Subject = "Error";
        myMail.Priority = MailPriority.Normal;
        myMail.BodyEncoding = System.Text.Encoding.UTF8;
        myMail.Body = body;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "mail";
        try
        {
            smtp.Send(myMail);
        }
        catch (SmtpException ex)
        {
            this.Label1.Text = "郵件發送失敗。\r\n"+ex.Message;
        }

    } 

 

至此,系統即可實現錯誤捕捉顯示,及郵件發生功能。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.