Default.aspx源代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="Form1" method="post" runat="server"> 
   <font face="宋体">您是第<%=Application["counter"]%>位访问者!</font>
    </form>
</body>
</html>
Global.asax源代码:
<%@ Application Src="Global.asax.cs" Inherits="webCounter.Global" %>Global.asax.cs源代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;
using System.IO;
namespace webCounter
{
    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();
        }
    }
}

解决方案 »

  1.   

    网上的一个例子,完全复制到VS 2010 英文版中,怎么也调试不成功。 jishu = (uint)Application["counter"];
     
     Use the "new" keyword to create an objcet instance..另外请教下各位的学习方法,我原来做过很长时间的Asp编程,现在想学习.Net和C#感觉,无从下手,编程思路什么的都懂,就是不知道代码往哪儿写。
      

  2.   

    断点 跟一下,看看Application["counter"] 是不是为空。