Does anyone have any suggestions on how I can do server side web tracking using MVC?

I would like to put some very simple web tracking for my MVC application. I would like to do this on the server side, and I am wondering if anyone has knowledge of some simple classes that I could use to help me with this.

I would like to keep track of the following things: the user's IP address, which page they requested, and which country they are in, as well as the DateTime stamp.

+1
source share
1 answer

Yes, you can intercept this in the controller for each request:

If you need the page that the user requested:

Request.RawUrl //Gives the current and complete URL the user requested

If you need the country from which it was requested, you can get the IP address of the user and then use the ready-made function to find where it is .

Request.UserHostAddress

You can also get all the route values ​​that the user has traveled; to get a better picture of how they got to where they are.

 public class MyController : Controller { public ActionResult Home() { var userIP = Request.UserHostAddress; var requestedUrl = Request.UserHostAddress; var routeValues = this.ControllerContext.RouteData.Route.GetRouteData(HttpContext); var requestedDateTime = DateTime.Now; } } 

Now you have to put it on every action, and it seems silly, so why not happen for everything that was done ?

 protected virtual void OnActionExecuting( ActionExecutingContext filterContext) { var userIP = filterContext.HttpContext.Request.UserHostAddress; var requestedUrl = filterContext.HttpContext.Request.UserHostAddress; var routeData = ((MvcHandler)filterContext.HttpContext.CurrentHandler).RequestContext.RouteData.Route.GetRouteData(filterContext.HttpContext); var requestedDateTime = DateTime.Now; } 
+2
source

All Articles