- 每一個網頁的 header 都要出現 "歡迎光臨"
- 每一個網頁的 footer 都要看到網頁出現的時間
- 當有 Error 時,網頁上要出現 "資料庫忙錄中"
<%@ Application Language="C#" %>
<script runat="server">
void Application_Start(Object sender, EventArgs e) {
}
void Application_OnBeginRequest(Object sender, EventArgs E)
{
Response.Write("歡迎光臨<br>");
}
protected void Application_OnEndRequest()
{
Response.Write("<hr>This page was served at " +DateTime.Now.ToString());
}
protected void Application_Error(Object sender, EventArgs e)
{
Response.Write("<span >");
Response.Write("資料庫忙碌中<hr></font>");
Response.Write("<font face=\"Arial\" size=\"2\">");
Server.ClearError();
}
</script>
<script runat="server">
void Application_Start(Object sender, EventArgs e) {
}
void Application_OnBeginRequest(Object sender, EventArgs E)
{
Response.Write("歡迎光臨<br>");
}
protected void Application_OnEndRequest()
{
Response.Write("<hr>This page was served at " +DateTime.Now.ToString());
}
protected void Application_Error(Object sender, EventArgs e)
{
Response.Write("<span >");
Response.Write("資料庫忙碌中<hr></font>");
Response.Write("<font face=\"Arial\" size=\"2\">");
Server.ClearError();
}
</script>
在 Application_Error 裡頭,也可以將 Error message 寫入 log file 或是以 email 呈現在網頁上,以下是我在網路上所看到的範例:
void Application_Error(object sender, EventArgs e)
{
string Message = "";
Exception ex = Server.GetLastError();
Message = "發生錯誤的網頁:{0}錯誤訊息:{1}堆疊內容:{2}";
Message = String.Format(Message, Request.Path + Environment.NewLine, ex.GetBaseException().Message + Environment.NewLine, Environment.NewLine + ex.StackTrace);
//寫入事件撿視器,方法一
System.Diagnostics.EventLog.WriteEntry("WebAppError", Message, System.Diagnostics.EventLogEntryType.Error);
//寫入文字檔,方法二
System.IO.File.AppendAllText(Server.MapPath(string.Format("Log\\{0}.txt", DateTime.Now.Ticks.ToString())), Message);
//寄出Email,方法三
//此方法請參考System.Net.Mail.MailMessage
//清除Error
Server.ClearError();
Response.Write("系統錯誤,請聯絡系統管理員!!");
}
{
string Message = "";
Exception ex = Server.GetLastError();
Message = "發生錯誤的網頁:{0}錯誤訊息:{1}堆疊內容:{2}";
Message = String.Format(Message, Request.Path + Environment.NewLine, ex.GetBaseException().Message + Environment.NewLine, Environment.NewLine + ex.StackTrace);
//寫入事件撿視器,方法一
System.Diagnostics.EventLog.WriteEntry("WebAppError", Message, System.Diagnostics.EventLogEntryType.Error);
//寫入文字檔,方法二
System.IO.File.AppendAllText(Server.MapPath(string.Format("Log\\{0}.txt", DateTime.Now.Ticks.ToString())), Message);
//寄出Email,方法三
//此方法請參考System.Net.Mail.MailMessage
//清除Error
Server.ClearError();
Response.Write("系統錯誤,請聯絡系統管理員!!");
}
而我們也可以利用 web.config file,依據錯誤的類型而導到不同的網頁,xml tag 是 <customErrors>,如下:
<customErrors defaultRedirect="GeneralError.htm" mode="on">
<error statuscode="404" redirect="FileNotFound.htm"/>
</customErrors>
<error statuscode="404" redirect="FileNotFound.htm"/>
</customErrors>
可以利用Application_BeginRequest,在使用瀏灠網頁時,記錄user id, current time(System.DateTime.Now) 以及 Path(Request.AppRelativeCurrentExecutionFilePath) 等等資訊,可以做為使用者的喜好分析或是檔案下載分析……
1 則留言:
讚啦。謝謝分享。
張貼留言