• How to Manage Session in ASP.NET MVC to Findout the Times our Webpage Visited

    by  • January 18, 2016 • Microsoft ASP.NET, Microsoft ASP.NET MVC • 0 Comments

    We know that HTTP is a stateless protocol, It means server retains no knowledge regarding web traffic. Now, how can we find-out, how many times people have visited our webpage during a particular time period. This can be done by using ASP.NET Session state. ASP.NET Session state enables us to store and retrieve the values and to retain state information for the particular session.

    Lets look at the code of ProductController.cs given below;


    In the above code, if session variable is null, we will initializing session variable to value 1 as in code line Session[“count”]=1; . Then we are storing the session variable value in an int variable name as count by type casting it to int type. Now we are using ViewBag object to store session variable as we know ViewBag is used to pass data from controller action to View. Whenever a user visited the page, there will a an increment in the session variable and we display this session variable in Index View.  Now lets look at the Index View code given below;


    The above code is very simple.  Here we use razor syntax to get the session value stored in the ViewBag object. ViewBag is used to pass data from controller action to view. Now look at the following output


    Let us refresh the page 3 more times and then look at the output.


    Best WordPress Themes and Plugins with Great Team and Support!

    Leave a Reply