Asp.net 2.0使用Global.asax制作网站计数器的一点心得
2006-10-25 02:55
966 查看
这两天一直心情不好今天尤其的坏,刚刚重新启动了我的那个秘密项目,找了点资料学习了Global.asax的使用方法,在项目里面加上了计数器的功能.
参考了很多资料,发现资料里面的东西确实很多不太适合自己,所以把自己的心得贴出来供大家参考
我给网站添加了Global.asax,App_Code文件夹下Global.asax.cs,文本文件Site_Counter.txt并写入数字0,代码分别如下:
Global.asax
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<%@ Application Inherits="Linker.Global" Language="C#" %>
Global.asax.cs
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Data;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Configuration;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.Security;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.WebControls;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.WebControls.WebParts;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.HtmlControls;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.IO;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace Linker
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// Global 的摘要说明
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public class Global : HttpApplication
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public Global()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// TODO: 在此处添加构造函数逻辑
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected void Application_Start(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 在应用程序启动时运行的代码
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
StreamReader rd = new StreamReader(Server.MapPath("Site_Counter.txt"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Application.Lock();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Application["Site_Counter"] = int.Parse(rd.ReadLine());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Application.UnLock();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
rd.Close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected void Session_Start(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 在新会话启动时运行的代码
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Application.Lock();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Application["Site_Counter"] = Convert.ToInt32(Application["Site_Counter"]) + 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Application.UnLock();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
StreamWriter wt = new StreamWriter(Server.MapPath("Site_Counter.txt"), false);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Application.Lock();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
wt.WriteLine(Application["Site_Counter"]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Application.UnLock();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
wt.Close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
然后在需要显示的页面显示系统 就可以了
比如简单的:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Label_Site_Counter.Text = Convert.ToString(Application["Site_Counter"]);
呵呵,大功告成,测试下吧,是不是已经看到了呢?
参考了很多资料,发现资料里面的东西确实很多不太适合自己,所以把自己的心得贴出来供大家参考
我给网站添加了Global.asax,App_Code文件夹下Global.asax.cs,文本文件Site_Counter.txt并写入数字0,代码分别如下:
Global.asax
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<%@ Application Inherits="Linker.Global" Language="C#" %>
Global.asax.cs
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Data;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Configuration;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.Security;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.WebControls;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.WebControls.WebParts;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.HtmlControls;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.IO;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace Linker
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// Global 的摘要说明
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public class Global : HttpApplication
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public Global()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// TODO: 在此处添加构造函数逻辑
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected void Application_Start(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 在应用程序启动时运行的代码
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
StreamReader rd = new StreamReader(Server.MapPath("Site_Counter.txt"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Application.Lock();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Application["Site_Counter"] = int.Parse(rd.ReadLine());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Application.UnLock();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
rd.Close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected void Session_Start(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 在新会话启动时运行的代码
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Application.Lock();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Application["Site_Counter"] = Convert.ToInt32(Application["Site_Counter"]) + 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Application.UnLock();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
StreamWriter wt = new StreamWriter(Server.MapPath("Site_Counter.txt"), false);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Application.Lock();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
wt.WriteLine(Application["Site_Counter"]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Application.UnLock();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
wt.Close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
然后在需要显示的页面显示系统 就可以了
比如简单的:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Label_Site_Counter.Text = Convert.ToString(Application["Site_Counter"]);
呵呵,大功告成,测试下吧,是不是已经看到了呢?
相关文章推荐
- Asp.net 2.0使用Global.asax制作网站计数器的一点心得
- Asp.net 2.0 使用SqlDataSource的一点心得
- 使用ASP.NET从零开始制作设计网站之十三
- Asp.net中Global.asax 的使用和事件
- 使用ASP.NET Global.asax 文件
- 使用ASP.NET Global.asax 文件--基础篇
- ASP.NET使用Global.asax进行全局错误处理
- 使用ASP.NET从零开始制作设计网站之五
- 在使用Asp.net制作网站的时候遇到的问题(一)
- 【转】ASP.NET Global.asax 文件使用方法 - .net
- 使用ASP.NET Global.asax 文件(转)
- asp.net Global.asax文件的使用
- 使用ASP.NET从零开始制作设计网站之四
- 使用ASP.NET从零开始制作设计网站之十二
- ASP.NET Core 2.0 使用支付宝PC网站支付
- ASP.NET Core 2.0 使用支付宝PC网站支付
- ASP.NET Core 2.0 使用支付宝PC网站支付实现代码(转)
- 使用ASP.NET Global.asax 文件
- asp.net中使用Global.asax文件中添加应用出错代码,写入系统日志文件或数据库
- 【翻译】使用VS2008开发运行在.net 2.0环境下的 ASP.NET AJAX 网站