gpt4 book ai didi

c# - asp.net c# 如何统计网站访问量

转载 作者:太空狗 更新时间:2023-10-29 21:33:36 24 4
gpt4 key购买 nike

如何在asp.net c#中统计网站访问量?

我正在使用下面的代码:

在 global.asax 页面中:

<%@ Application Language="C#" %>

void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Application["NoOfVisitors"] = 0;
}

void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
Application.Lock();
Application["NoOfVisitors"] = (int)Application["NoOfVisitors"] + 1;
Application.UnLock();
}

在 .aspx 页面中:

<asp:Label runat="server" ID="lbluser" />

在 .aspx.cs 中:

protected void Page_Load(object sender, EventArgs e)
{
lbluser.Text = Application["NoOfVisitors"].ToString();
}

应用程序计数器每隔一小时重置为 0 ...我在计算用户数量时哪里出错了?

最佳答案

应用程序状态是易变的。检查this MSDN articule :

When using application state, you must be aware of the following important considerations:

  • ...

  • Volatility Because application state is stored in server memory, it is lost whenever the application is stopped or restarted. For example, if the Web.config file is changed, the application is restarted and all application state is lost unless application state values have been written to a non-volatile storage medium such as a database.

所以你不应该用它来保存这种你想长期保存的数据。因为应用程序池会不时重置。我怀疑您不想在发生这种情况时重置您的访问者数量。

您需要某种数据存储,可以在您的应用程序未运行时保留您的数据。

这里有一些选择:

关于c# - asp.net c# 如何统计网站访问量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26189600/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com