ASP.NET Visitors counter

社会主义新天地 提交于 2019-12-11 12:04:19

问题


I am creating a counter for my webpage. What wan't to achieve is that every time user visits my asp.net application, it stores his data into database. I am using Global.asax and event Application_Start. Here is my code .

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        RegisterRoutes(RouteTable.Routes);

        WebpageCounter.SaveVisitor(new WebpageVisitor()
        {
            VisitorIP = HttpContext.Current.Request.UserHostAddress,
            VisitedOn = DateTime.Now
        });
    }

But it never stores anything into database. The function SaveVisitor has been tested and it is functional.

Any suggestions ?


回答1:


Application_Start is runs only when process created - not every visit.

You can use Application_BeginRequest instead.




回答2:


Application_Start() is only called once for the lifetime of the application domain - not for every request to your site. Also see "ASP.NET Application Life Cycle Overview for IIS 5.0 and 6.0"




回答3:


Code for the code behind:

C#

    protected void Page_Load(object sender, EventArgs e)
    {
        this.countMe();

        DataSet tmpDs = new DataSet();
        tmpDs.ReadXml(Server.MapPath("~/counter.xml"));

        lblCounter.Text = tmpDs.Tables[0].Rows[0]["hits"].ToString();
    }

    private void countMe()
    {
        DataSet tmpDs = new DataSet();
        tmpDs.ReadXml(Server.MapPath("~/counter.xml"));

        int hits = Int32.Parse(tmpDs.Tables[0].Rows[0]["hits"].ToString());

        hits += 1;

        tmpDs.Tables[0].Rows[0]["hits"] = hits.ToString();

        tmpDs.WriteXml(Server.MapPath("~/counter.xml"));

    }

VB.NET

    Protected Sub Page_Load(sender As Object, e As EventArgs)
        Me.countMe()

        Dim tmpDs As New DataSet()
        tmpDs.ReadXml(Server.MapPath("~/counter.xml"))

        lblCounter.Text = tmpDs.Tables(0).Rows(0)("hits").ToString()
    End Sub

    Private Sub countMe()
        Dim tmpDs As New DataSet()
        tmpDs.ReadXml(Server.MapPath("~/counter.xml"))

        Dim hits As Integer = Int32.Parse(tmpDs.Tables(0).Rows(0)("hits").ToString())

        hits += 1

        tmpDs.Tables(0).Rows(0)("hits") = hits.ToString()

        tmpDs.WriteXml(Server.MapPath("~/counter.xml"))
    End Sub

XML file will look like this:

<?xml version="1.0" encoding="utf-8" ?>
<counter>
  <count>
     <hits>0</hits>
  </count>




回答4:


This information can be logged by IIS, and then queried/transformed using the excellent logparser. You could also put Google Analytics on your site - its free version is sufficient for all but the busiest sites. If you still feel the need to do this yourself, then Application_BeginRequest is a better place to record this.

EDIT: You could implement it as a module, like the MSDN Custom Module Walkthrough and then your app could be a little more modular



来源:https://stackoverflow.com/questions/7568497/asp-net-visitors-counter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!