ASP.NET Visitors counter


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 ?



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

You can use Application_BeginRequest instead.


Application_Start() is only called once for the lifetime of the application domain - not for every request to your site. Also see <a href="http://msdn.microsoft.com/en-us/library/ms178473.aspx" rel="nofollow">"ASP.NET Application Life Cycle Overview for IIS 5.0 and 6.0"</a>


Code for the code behind:


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")); }


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>


This information can be logged by IIS, and then queried/transformed using the excellent <a href="http://technet.microsoft.com/en-us/scriptcenter/dd919274" rel="nofollow" title="logparser 2.2">logparser</a>. 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.

<strong>EDIT</strong>: You could implement it as a module, like <a href="http://msdn.microsoft.com/en-us/library/ms227673%28v=VS.100%29.aspx" rel="nofollow" title="msdn">the MSDN Custom Module Walkthrough</a> and then your app could be a little more modular


  • Difference between “::” “.” and “->” in c++
  • How to check if instance exists if variable not existing?
  • How invoke APEX method from custom button
  • No connection string named 'MyAppDbContext' could be found in the application config file
  • OpenCover takes much longer to run than the nunit-console
  • .NET serialization: Best practice to map classes to elements and properties to attributes?
  • Share session on subdomains in php
  • Installing ngpsql to use PostgreSQL in PowerBI
  • ruby on rails TypeError in Users#show - Cannot visit Like
  • Angularjs hide and show according to screen size responsive
  • yii2 create translated URLs
  • Laravel phpunit always 404
  • An unexpected error has been detected by HotSpot Virtual Machine
  • php show all images in directory and sort by last modified
  • SQL getting multiple columns into one column
  • Changing Jupyter Notebook start up folder by modifying “start in” not working any more
  • netsh acl setting (need alternative method - registry settings?)
  • Sending keystrokes/mouse clicks to a Java program with Autohotkey
  • how does System.Web.HttpRequest::PathInfo work?
  • Object and struct member access and address offset calculation
  • Not able to aggregate on nested fields in elasticsearch
  • ViewController With Transparent Background Entering Current ViewController With Push Transition
  • How can I sort a a table with VBA with given text condition?
  • How to match http request and response using Jersey ContainerRequestFilter and ContainerResponseFilt
  • Meteor: Do Something On Email Verification Confirmation
  • Google Custom Search with transparent background
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • Different response to non-authenticated users and AJAX calls
  • Incrementing object id automatically JS constructor (static method and variable)
  • Perl system calls when running as another user using sudo
  • Knitr HTML Loop - Some HTML output, some R output
  • Why winpcap requires both .lib and .dll to run?
  • Free memory of cv::Mat loaded using FileStorage API
  • How to set the response of a form post action to a iframe source?
  • Understanding cpu registers
  • How do I configure my settings file to work with unit tests?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • unknown Exception android
  • How do I use LINQ to get all the Items that have a particular SubItem?