3

Is there an easy way to log every page hit by any user. I am thinking this will sit within the global.asax.cs file, so that I can write to a db table the URL of the page being hit.

Tom Ax
  • 566
  • 5
  • 23

2 Answers2

4

I've found a way to complete this problem which seems to fit my purpose.

I use the PostAuthenticateRequestHandler method, as it is called for every page hit. I ignore any empty path and just "/" as these are not actual pages hit.

//in global.asax.cs file

private void PostAuthenticateRequestHandler(object sender, EventArgs e)
{
   ///...///

   string extension = this.Context.Request.CurrentExecutionFilePathExtension;
   string path = this.Context.Request.CurrentExecutionFilePath;

   if (extension == string.Empty && path != "/")
   {
   PageVisitedLogModel pageVisitedLogModel = new PageVisitedLogModel
   {
       DateVisited = DateTime.Now,
       IPAddress = this.Context.Request.UserHostAddress,
       PageURL = this.Context.Request.RawUrl,
       Username = this.Context.User.Identity.Name
   };

   //then writes to log
   DataHelper.UpdatePageVisitedLog(pageVisitedLogModel);
}
}
Tom Ax
  • 566
  • 5
  • 23
0

One way to do this would be to use a global action filter, as in this example. This allows you to run some code for any action in your solution.

Rob West
  • 5,189
  • 1
  • 27
  • 42
  • What happens if the action output is cached? will the action filter will work at that time? – VJAI Jun 28 '12 at 14:16
  • No, if you use the standard output caching this would not fire, but there is a solution: http://stackoverflow.com/questions/10990337/working-with-the-output-cache-and-other-action-filters – Rob West Jun 28 '12 at 15:56