Asp.net 网站访问者计数在服务器(云窗口服务器)上不起作用。你怎么修理它?
总之,我在服务器上运行asp.net发布文件时遇到问题。网站访问者计数记录在服务器上不起作用。但它在我的本地IIS服务器上运行顺利。如何修复它? 它是Global.asax.cs中的代码Asp.net 网站访问者计数在服务器(云窗口服务器)上不起作用。你怎么修理它?,asp.net,Asp.net,总之,我在服务器上运行asp.net发布文件时遇到问题。网站访问者计数记录在服务器上不起作用。但它在我的本地IIS服务器上运行顺利。如何修复它? 它是Global.asax.cs中的代码 private static int totalNumberOfUsers = 0; protected void Application_Start(Object sender, EventArgs e) { //Code that run on application start up
private static int totalNumberOfUsers = 0;
protected void Application_Start(Object sender, EventArgs e)
{
//Code that run on application start up
Application["NumberofVisitors"] = 0;
totalNumberOfUsers = 0;
}
protected void Session_Start(Object sender, EventArgs e)
{
//Code that run on a new session is started
Application.Lock();
totalNumberOfUsers = (int)Application["NumberofVisitors"] + 1;
Application.UnLock();
}
protected void Session_End(Object sender, EventArgs e)
{
Application.Lock();
totalNumberOfUsers -= 1;
Application.UnLock();
}
public static int TotalNumberOfUsers { get { return totalNumberOfUsers; } }
它是view.aspx
<div class="row">
<div class="col-md-1"></div>
<div class="col-md-4">
<div class="row">
<div class="col-sm-4">
<asp:Label ID="lblVisitorRecord" runat="server" CssClass="btn btn-primary" style="font-size:medium;">
</asp:Label>
</div>
<div class="col-sm-4">
</div>
</div>
</div>
<div class="col-md-4"></div>
</div>
protected void Page_Load(object sender, EventArgs e)
{
int totalNumberOfUsers = Global.TotalNumberOfUsers;
lblVisitorRecord.Text = "Visitors : " + totalNumberOfUsers;
if (!IsPostBack)
{
}
}
这是web.config中的会话超时
<sessionState mode="SQLServer" allowCustomSqlDatabase="true"sqlConnectionString="MainDB" cookieless="false" timeout="40" />
正如您已经声明的
totalNumberOfUsers
为static
您不需要应用程序[“NumberofVisitors”]
。您只需增加或减少totalNumberOfUsers
的值,如下所示
private static int totalNumberOfUsers = 0;
protected void Application_Start(Object sender, EventArgs e)
{
//Code that run on application start up
totalNumberOfUsers = 0;
}
protected void Session_Start(Object sender, EventArgs e)
{
//Code that run on a new session is started
totalNumberOfUsers++;
}
protected void Session_End(Object sender, EventArgs e)
{
totalNumberOfUsers--;
}
public static int TotalNumberOfUsers { get { return totalNumberOfUsers; } }
totalNuberOfUsers将始终设置为1,因为您从不增加应用程序[“NumberofVisitors”]。0+1始终是1。。。不管怎么说,奇怪的逻辑是,您不需要锁定/解锁来更改共享变量的值,您共享的代码似乎不完整?是的!。它的代码在我的本地IIS服务器上顺利运行。用户总数已经增加。但它并没有在Windows server中增加。是的,我将使用您的代码进行测试。谢谢您的回答。