我写了一个简单的计数器,在global.asax中,代码如下:
public class Global : System.Web.HttpApplication
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; public Global()
{
InitializeComponent();
}

protected void Application_Start(Object sender, EventArgs e)
{


StreamReader read = new StreamReader(Server.MapPath("Count.txt"));
string str = read.ReadLine(); uint count = uint.Parse(str); object obj = count; Application["Count"] = obj; read.Close();

}
 
protected void Session_Start(Object sender, EventArgs e)
{

Application.Lock();
uint count = (uint)Application["Count"];
count++; object obj = count;
Application["Count"] = obj;
Application.UnLock();
// StreamWriter writer = new StreamWriter(Server.MapPath("Count.txt"));
// writer.WriteLine(Application["Count"].ToString());
// writer.Close();
} protected void Application_BeginRequest(Object sender, EventArgs e)
{ } protected void Application_EndRequest(Object sender, EventArgs e)
{ } protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{ } protected void Application_Error(Object sender, EventArgs e)
{ } protected void Session_End(Object sender, EventArgs e)
{ } protected void Application_End(Object sender, EventArgs e)
{
StreamWriter writer = new StreamWriter(Server.MapPath("Count.txt"));
writer.WriteLine(Application["Count"].ToString());
writer.Close(); }

#region Web 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.components = new System.ComponentModel.Container();
}
#endregion
}
可是,我发现我每次跳转页面都会触发"Application_Start"和"Session_Start"事件。但是这两个事件不是当第一次请求和产生一个新的会话才会触发吗?请高手指点!

解决方案 »

  1.   

    是不是要在web.config文件里面设置一下?或者其他什么地方要设置一下?我用的就是VS.2003M默认的设置来写的,没有改任何的东西,真的不是很明白!
      

  2.   

    有无装防火墙,NORTON之类的?
      

  3.   

    我试了很多次,发现只要把这个站点放到wwwroot文件夹里面(也就是IIS默认站点的文件夹),并且在IIS的虚拟目录中指向这个文件夹,就不会出现这个情况。但是为什么会这样,我也不是很明白。难道虚拟目录的文件夹放到其他地方和放到站点根目录里会有什么不同吗?我对比了很久也没有找到问题所在,真是伤透了脑筋!
      

  4.   

    还是没有解决,请孟子大哥和webdiyer等高手帮我看看吧!
      

  5.   

    protected void Application_Start(Object sender, EventArgs e)
    {
    Application["Application_Start"]="A";
    }
     
    protected void Session_Start(Object sender, EventArgs e)
    {
    Application["Application_Start"]="S";
    }不是每次跳转页面都会触发"Application_Start"和"Session_Start"事件你可以试一下..
    private void Page_Load(object sender, System.EventArgs e)
    {
    Response.Write(Application["Application_Start"].ToString());
    Application["Application_Start"]="M";
    Response.Write(Application["Application_Start"].ToString());
    }
    第一次运行的时候,输出SM 
    因为Session_Start的application 把Application_Start的值给盖掉了.
    刷新面一下.会输出MM
      

  6.   

    我想这种情况应该算特例.
    试想,大多数的网站计数器,都是靠session-start这个事件来进行加1,如果这种情况出现,那岂不乱套了?
    我的网站肯定没有这种问题.
    所以分析这是楼主当前那台服务器的RPWT.
    建议:换一个服务器试试