计算访客数在使用ASP.Net和C#网页 [英] Count Number of Visitors in WebSite using ASP.Net and C#

查看:114
本文介绍了计算访客数在使用ASP.Net和C#网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要跟踪的游客数量来我的网站。

I want to keep track of the number of visitors to my site.

我尝试以下code在Global.asax类,

I tried the following code in the Global.asax class,

<script runat="server">

  public static int count = 0;
  void Application_Start(object sender, EventArgs e) 
  {
    Application["myCount"] = count;
  }

  void Session_Start(object sender, EventArgs e) 
  {
    count = Convert.ToInt32(Application["myCount"]);
    Application["myCount"] = count + 1;
  }

</script>

我在检索aspx页面的价值如下:

I am retrieving the value in the aspx page as follows:

protected void Page_Load(object sender, EventArgs e)
{
  int a;
  a = Convert.ToInt32((Application["myCount"]));
  Label4.Text = Convert.ToString(a);
  if (a < 10)
    Label4.Text = "000" + Label4.Text ;
  else if(a<100)
    Label4.Text = "00" + Label4.Text;
  else if(a<1000)
    Label4.Text = "0" + Label4.Text;
}

上面的编码工作正常。它产生正确的游客,但问题是,当我重​​新启动我的系统中,计数变量再次从0开始在逻辑上是错误的。

The above coding works fine. It generates the Visitors properly but the problem is when I restart my system, the count variable again starts from 0 which logically wrong.

欲计数的值由1从最后的计数值递增。

I want the value of count to be incremented by 1 from the last count value.

因此​​,谁能告诉我如何完成这个任务?

So can anyone tell me how to accomplish this task?

请帮助我!
在此先感谢!

Please help me out! Thanks in advance!

推荐答案

如果你想计数保持在递增重新启动应用程序,你需要的地方存储值 - 在数据库或其他地方的文件,并加载应用程序启动时价值高达

If you want the count to keep incrementing over application restarts, you'll need to store the value somewhere - in a database or a file somewhere, and load that value up when the application starts.

此外,您还可以使用以下,以确保您显示计数总是至少4个字符:

Also, you can use the following to ensure your displayed count is always at least 4 characters:

int a;
a = Convert.ToInt32(Application["myCount"]);
Label4.Text = a.ToString("0000");

请参阅自定义数字格式字符串,获取更多信息。

See Custom Numeric Format Strings for more info.


修改回应置评

就个人而言,我推荐使用比写入文件系统的数据库中,至少有以下几个原因:

Personally, I'd recommend using a database over writing to the file system, for at least the following reasons:


  1. 根据您的主机上,建立一个数据库可能比使你的文件系统的写访问要容易得多。

  2. 使用一个数据库将允许你把它存储为一个 INT 而非字符串

  3. 在交通繁忙,你必须与多个线程试图打开写访问的文本文件的问题 - 这将导致该文件的锁,并造成瓶颈,你不需要

各种资源会告诉你如何从code连接到一个数据库,一个良好的开端将是这样的如何:连接到SQL Server ,并寻找到下的方法<一个href=\"http://msdn.microsoft.com/en-us/library/ms971491.aspx#commandbuilder%20post%20edit%5Ftopic4\">What是替代的关于如何查询和更新数据库详细信息。

Various resources will tell you how to connect to a database from your code, a good place to start would be this How To: Connect to SQL Server, and looking into the methods under "What are the alternatives" for details on how to query and update the database.

这篇关于计算访客数在使用ASP.Net和C#网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆