本文講述了如何利用ASP.NET技術,製作首頁計數器。

來源:互聯網
上載者:User
設計構思

計數器的核心工作就是想辦法將訪問的次數記錄下來,並且能夠方便的讀出資料記錄。在此應用中,擬建立四個檔案,一個是webform1.aspx,主要是用於顯示訪問次數記錄,一個counter.txt檔案用於儲存訪問次數記錄,還有global.asax和global.asax.cs,這兩個檔案是核心檔案,主要是負責響應事件和讀寫檔案。因此,程式必須具有開啟檔案,讀檔案,累加數值,寫檔案等功能。同時,還需注意:在進行數值累加時,不能象ASP中的那樣寫成

application(“counter”)=application(“counter”)+1 

因為數實值型別不能和對象做數學運算。經過以上的思考,我們基本就可以編寫代碼了,但是在完成編寫之前,還應瞭解以下的相關知識。 

相關知識

1. Global.asax檔案

Global.asax檔案也稱為ASP.NET應用程式檔案,它一般被放在根目錄下。此檔案中的代碼不產生使用者介面,也不相應單個頁面的請求。它主要是負責處理Application_Start,Application_End,Session_Start和Session_End事件的。

2. Application對象及其事件

Application 對象來自HttpApplictionStat 類。它可以在多個請求、串連之間共用公用資訊,也可以在各個請求串連之間充當資訊傳遞的管道。此對象的生命週期起於IIS開始運行並且有人開始串連時,終止於IIS關閉或者若干時間內無人串連(預設為20分鐘)。當Application 對象的生命週期開始時,Application_Start 事件會被啟動,當Application對象的生命週期結束時Application_End 事件會被啟動。 

3. Session對象及其事件

Session對象有著與Application類似的事件:Session_Start和Session_End事件。當有一個新使用者訪問應用程式時,就會立刻觸發Session_Start事件。當某個使用者停止了訪問或者程式執行了Session.Abandon方法,就會觸發Session_End事件。

4. Application和Session對象比較

Session對象與Application對象有些相似,但其範圍有更大的限制。Application對象是針對所有使用者都生效,而Session對象則相反,每個使用者都有自己的Session對象,它的生命週期起始於伺服器產生對使用者請求頁面的相應,終止於使用者斷開與伺服器的串連。Application對象不會象Session對象那樣當一個新使用者請求就觸發事件,Application對象的事件只觸發一次,就是在第一個使用者的第一個請求時。一個Application_End事件肯定發生在Session_End事件之後,Application_End事件只有在伺服器停止工作或Application_End事件卸載時才觸發。 

程式部分

首先建立一個文字檔counter.txt,開啟檔案輸入一個大於0的整數作為訪問記錄的初始值。

面我們就可以正式的編寫計數器的程式了。

listing 1是webform1.aspx,主要是用於顯示從檔案中讀出的訪問次數的記錄。由於在整個應用程式生命週期中,Application 對象都是有效,所以在不同的頁面中都可以對它進行存取,就像使用全域變數一樣方便。

在代碼中,使用<%=Application["counter"]%>來表示訪問次數記錄。

程式碼如下:

listing1 -----webform1.aspx-----

<%@ Page language="c#" Src="WebForm1.aspx.cs" Inherits="counter1.WebForm1" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<HTML>

<HEAD>

<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">

<meta name="CODE_LANGUAGE" Content="C#">

</HEAD>

<body>

<form id="Form1" method="post" runat="server">

<FONT face="宋體">您是第<%=Application["counter"]%>位訪問者!</FONT>

</form>

</body>

</HTML> 

Listing 2和listing3是global.asax和global.asax.cs檔案代碼,當執行webform1.aspx檔案之前會執行它們。在global.asax.cs檔案中,定義了一些事件和其響應代碼,主要是用於讀寫檔案和數值累加。

Listing 2 -----global.asax----

<%@ Application Src="Global.asax.cs" Inherits="counter2.Global" %>

listing 3 -----global.asax.cs-----

using System;

using System.Collections;

using System.ComponentModel;

using System.Web;

using System.Web.SessionState;

using System.IO ;

namespace counter2

{

public class Global : System.Web.HttpApplication

{

protected void Application_Start(Object sender, EventArgs e)

{

uint count=0;

StreamReader srd;

//取得檔案的實際路徑

string file_path=Server.MapPath ("counter.txt");

//開啟檔案進行讀取

srd=File.OpenText (file_path);

while(srd.Peek ()!=-1)

{

string str=srd.ReadLine ();

count=UInt32.Parse (str);

}

object obj=count;

Application["counter"]=obj;

srd.Close ();

}

protected void Session_Start(Object sender, EventArgs e)

{

Application.Lock ();

//數值累加,注意這裡使用了裝箱(boxing)

uint jishu=0;

jishu=(uint)Application["counter"];

jishu=jishu+1;

object obj=jishu;

Application["counter"]=obj;

//將資料記錄寫入檔案

string file_path=Server.MapPath ("counter.txt");

StreamWriter fs=new StreamWriter(file_path,false);

fs.WriteLine (jishu);

fs.Close ();

Application.UnLock ();

}

protected void Application_BeginRequest(Object sender, EventArgs e)

{

}

protected void Application_EndRequest(Object sender, EventArgs e)

{

}

protected void Session_End(Object sender, EventArgs e)

{

}

protected void Application_End(Object sender, EventArgs e)

{

//裝箱

uint js=0;

js=(uint)Application["counter"];

//object obj=js;

//Application["counter"]=js;

//將資料記錄寫入檔案

string file_path=Server.MapPath ("counter.txt");

StreamWriter fs=new StreamWriter(file_path,false);

fs.WriteLine(js);

fs.Close ();

}

}

經過以上的討論,一個簡單的首頁計數器就完成了。其核心就是以文字模式進行檔案讀寫。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.