<%@ Application Language="C#" %>
<script RunAt="server">
void Application_Start(object sender, EventArgs e)
{
// 在應用程序啓動時運行的代碼
System.Timers.Timer timehtml = new System.Timers.Timer(1000 * 600);
timehtml.AutoReset = true;
timehtml.Enabled = true;
timehtml.Elapsed += new System.Timers.ElapsedEventHandler(BuildHtml);
}
void Application_End(object sender, EventArgs e)
{
// 在應用程序關閉時運行的代碼
}
/// <summary>
/// 生成靜態頁面
/// </summary>
void BuildHtml(object sender, System.Timers.ElapsedEventArgs e)
{
string htmlcode = getUrltoHtml("http://localhost/Default.aspx");
string path = AppDomain.CurrentDomain.BaseDirectory + "Default.htm";
System.IO.StreamWriter sw = new System.IO.StreamWriter(path, false, System.Text.Encoding.UTF8);
sw.Write(htmlcode);
sw.Close();
System.Threading.Thread.Sleep(600000);
}
/// <summary>
/// 傳入URL返回網頁的html代碼
/// </summary>
/// <param name="Url"></param>
/// <returns></returns>
string getUrltoHtml(string Url)
{
string errorMsg = "";
try
{
System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);
System.Net.WebResponse wResp = wReq.GetResponse();
System.IO.Stream respStream = wResp.GetResponseStream();
System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.UTF8);
return reader.ReadToEnd();
}
catch (System.Exception ex)
{
errorMsg = ex.Message;
}
return "";
}
void Application_Error(object sender, EventArgs e)
{
// 在出現未處理的錯誤時運行的代碼
}
void Session_Start(object sender, EventArgs e)
{
// 在新會話啓動時運行的代碼
Session.Timeout = 5; //設置Session的有效時間,可根據需要修改
Application.Lock();
Application["online"] = Convert.ToInt32(Application["online"]) + 1;
Application.UnLock();
}
void Session_End(object sender, EventArgs e)
{
// 在會話結束時運行的代碼
// 注意: 只有在 Web.config 文件中的 sessionstate 模式設置爲
// InProc 時,纔會引發 Session_End 事件。如果會話模式設置爲 StateServer
// 或 SQLServer,則不會引發該事件。
Application.Lock();
Application["online"] = Convert.ToInt32(Application["online"]) - 1;
Application.UnLock();
}
</script>