注:最近在復習ASP.NET,為了加深印象,會制作一些小的demo程序,分享給大家。
1 新建ASP.NET網站,編輯Global.asax文件,修改后的文件內容如下所示。
%@ Application Language="C#" %>
script runat="server">
void Application_Start(object sender, EventArgs e)
{
// 在應用程序啟動時運行的代碼
Application["CurrentUserCount"] = 0;
}
void Application_End(object sender, EventArgs e)
{
// 在應用程序關閉時運行的代碼
}
void Application_Error(object sender, EventArgs e)
{
// 在出現未處理的錯誤時運行的代碼
}
void Session_Start(object sender, EventArgs e)
{
// 在新會話啟動時運行的代碼
Application.Lock();
Application["CurrentUserCount"] = (int)Application["CurrentUserCount"] + 1;
Application.UnLock();
}
void Session_End(object sender, EventArgs e)
{
// 在會話結束時運行的代碼。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式設置為 InProc 時,才會引發 Session_End 事件。
// 如果會話模式設置為 StateServer
// 或 SQLServer,則不會引發該事件。
Application.Lock();
Application["CurrentUserCount"] = (int)Application["CurrentUserCount"] - 1;
Application.UnLock();
}
/script>
2 修改Web.config文件,增加如下配置節點,新增的配置節點位system.web>/system.web>節點下。
復制代碼 代碼如下:
sessionState mode="InProc" timeout="1" cookieless="false"/>
3 在Default.aspx文件中添加一個標簽來顯示當前在線人數。
復制代碼 代碼如下:
protected void Page_Load(object sender, EventArgs e)
{
this.Label1.Text = Application["CurrentUserCount"].ToString();
}
4 先后使用IE和Chrome瀏覽器訪問應用,得到下圖所示結果。

您可能感興趣的文章:- 用c#獲得當前用戶的Application Data文件夾位置
- ASP.NET內置對象之Application對象
- ASP.NET 使用application與session對象寫的簡單聊天室程序
- ASP.Net的Application介紹
- ASP.NET 中的Application詳解
- ASP.NET中Application全局對象用法實例淺析
- ASP.NET中application對象的使用介紹
- ASP.NET中application對象的使用介紹
- ASP.NET C#中Application的用法教程