我写了一个简单的计数器,在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"事件。但是这两个事件不是当第一次请求和产生一个新的会话才会触发吗?请高手指点!
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"事件。但是这两个事件不是当第一次请求和产生一个新的会话才会触发吗?请高手指点!
解决方案 »
- 急啊,在线急求关于利用IIS7实现访问某一页面需要集成windows认证的问题
- 用com组件导出excel时,能不能不保存在服务器上,直接输出到客户端
- 水晶报表命名空间问题
- TextBox 弹出GridView!
- 请牧月天涯进来
- 客戶通過http協議往數據庫上傳圖片時要注意的一個提示!
- 前台Aspx布面与后台CS页面互动问题~!
- ArrayList的问题
- GridView 中隐藏列 Visible=false会不能正常取值 ???????
- 如何判断浏览器点击了“后退”按钮?
- 谁知道微软的.NET报表制作工具在哪儿下(RDL)
- 为什么DropDownList在客户端改变了数据源之后,在服务器端没有改变
{
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
试想,大多数的网站计数器,都是靠session-start这个事件来进行加1,如果这种情况出现,那岂不乱套了?
我的网站肯定没有这种问题.
所以分析这是楼主当前那台服务器的RPWT.
建议:换一个服务器试试