显示访问我们的Web应用程序的访问者数量 [英] display Number of visiters visited our webapplication

查看:87
本文介绍了显示访问我们的Web应用程序的访问者数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的静态asp.net网站上显示访问的用户数(不是当前的在线用户).






目前,我尝试使用global.asax文件进行显示.

代码在global.asax文件中,如下所示:

I need to display no of users visited(Not current online users) in my static asp.net website.






Currently I tried to display using global.asax file.

The code is in global.asax file like below:

protected void Application_Start(object sender, EventArgs e)
       {

           Application["OnlineUsers"] = 0;



       }

 protected void Session_Start(object sender, EventArgs e)
       {

           Application.Lock();
           Application["OnlineUsers"] = (int)Application["OnlineUsers"] + 1;
           Application.UnLock();



       }




但是此代码无法正常工作.请任何人都需要完整的




But This code not working properly.Please any one do need full

推荐答案

看看以下线程:
http://www. aspdotnetfaq.com/Faq/How-to-show-number-of-online-users-visitors-for-ASP-NET-website.aspx [免费点击计数器 [ ^ ]

在与整个应用程序交互的 global.asax 文件中编写代码.
示例代码如下:
Have a look on following thread:
http://www.aspdotnetfaq.com/Faq/How-to-show-number-of-online-users-visitors-for-ASP-NET-website.aspx[^]
Free hit counter[^]

Write code in global.asax file which interacts with the entire application.
The sample code is given below:
void Application_OnStart(Object Sender, EventArgs E)
        {
            Application["CurrentUsers"] = 0;
        }
        void Session_OnStart(object Sender, EventArgs E)
        {
            Application.Lock();
            Application["CurrentUsers"] = System.Convert.ToInt32(Application["CurrentUsers"]) + 1;
            Application.UnLock();
        }
        void Session_OnEnd(object Sender, EventArgs E)
        {
            Application.Lock();
            Application["CurrentUsers"] = System.Convert.ToInt32(Application["CurrentUsers"]) - 1;
            Application.UnLock();
        }



试试这个:
像这样的global.asax文件



Try this one:
global.asax file for that like

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

    }


void Session_Start(object sender, EventArgs e)
  {
      // Code that runs when a new session is started
      Session["Users"] =Convert.ToInt32( Application["OnlineUsers"])+ 1;
      Application["OnlineUsers"] = Session["Users"];
  }


页面加载:


page load:

Response.Write(Session["Users"].ToString());


在您的登录屏幕中

您可以添加以下代码

int Count = 0
Application ["OnlineUsers"] = Count + 1;
in your login Screen

you can add below code

int Count=0
Application["OnlineUsers"] = Count+1;


您需要编写XML或其他内容,否则在重新启动或发生任何事情时都会丢失总数.

样本文件操作以存储计数(您可以将其放入Global.asax中):
you need to write it XML or something, otherwise you lose total counts on any restart or anything.

Sample File opration to store counts(you can put it in Global.asax):
public static int Visitors =0; //access this member whenever you need count.

  void Application_Start(object sender, EventArgs e) 
  {
    LogVistorCount();
  }

  void Session_Start(object sender, EventArgs e) 
  {
    LogVistorCount();
  }

 private void LogVistorCount()
 {
       string strXmlCreateSample = "" +
                                                    "<counter>" +
                                                        "<count>" +
                                                          "<hits>" +
                                                          "</hits>" +
                                                        "</count>" +
                                                    "</counter>";
      
      string filepath = Server.MapPath("~/VistorsCount.xml");
      try
      {
           DataSet tmpDs = new DataSet();
           
          if(!System.IO.File.Exists(filepath)
          {
             XmlDocument doc = new XmlDocument();
             doc.LoadXml(strXmlCreateSample);
             doc.Save(filepath);
          }
    
           tmpDs.ReadXml(filepath);
    
           int hits = Int32.Parse(tmpDs.Tables[0].Rows[0]["hits"].ToString());
    
           hits += 1;
           Visitors = hits;
           tmpDs.Tables[0].Rows[0]["hits"] = hits.ToString();
    
           tmpDs.WriteXml(filepath);
       }
       catch{}

  }


这篇关于显示访问我们的Web应用程序的访问者数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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