How to capture page views in MVC

721 views Asked by At

What is the best way to capture the number of page views and update the database in MVC? I have gone through what is the best way to capture page views per user in asp.net-mvc solutions in which using action filters are suggested. But what if we want to track the pageviews for each unique parameter passed to the action method - like how stackoverflow calculates the page visits for each question ? I think, we cannot use action filters in this case as page views differs for different parameters.

Is there any solution available otherthan incrementing the database value each time the page is viewed ?

1

There are 1 answers

0
KnightFox On

why can you not use action filters

    public class MyActionFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var actionName = filterContext.ActionDescriptor.ActionName;
            var controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
            var parameterICareAbout = filterContext.ActionParameters["parameterICareAbout "];
            //write to database using parameterICareAbout
        }
     }

As for not storing value on the database everytime, you could set up a Singleton class in your app that keeps track of all these requests and then bulk writes them to the database at some cadence/ or writes to the database during the application spin down process