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

相關(guān)文章
ASP.NET2.0 WebRource,開發(fā)微調(diào)按鈕控件
ASP.NET2.0 WebRource,開發(fā)微調(diào)按鈕控件...2006-09-09
.NET實(shí)現(xiàn)魔方游戲(一)之任意階魔方的表示
這篇文章主要介紹了.NET實(shí)現(xiàn)魔方游戲(一)之任意階魔方的表示 的相關(guān)資料,需要的朋友可以參考下2016-02-02
asp.net為網(wǎng)頁動態(tài)添加description描述信息的方法
這篇文章主要介紹了asp.net為網(wǎng)頁動態(tài)添加description描述信息的方法,涉及asp.net動態(tài)操作網(wǎng)頁元素的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-04-04
ASP.NET Core 2.0 使用支付寶PC網(wǎng)站支付實(shí)現(xiàn)代碼
這篇文章主要介紹了ASP.NET Core 2.0 使用支付寶PC網(wǎng)站支付實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-10-10
詳解Spring Boot 中使用 Java API 調(diào)用 lucene
這篇文章主要介紹了詳解Spring Boot 中使用 Java API 調(diào)用 lucene,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
ASP .NET Core API發(fā)布與部署以及遇到的坑和解決方法
這篇文章主要介紹了ASP .NET Core API發(fā)布與部署以及遇到的坑和解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08

