tags:

views:

93

answers:

2

Hi,

Do anyone knows how to do the View count for a specific page. Like in StackOverflow, in the question list you would see the View count which describes how many times the question was viewed.

I would like to know how to do this in asp.net mvc. I already have an idea but im not sure if right.

I need an expert advice... thanks

+1  A: 

A common way to do this is to log the number of times a page is requested in a database. You can then add a property to your domain object which can be populated in the normal manner by your data access layer, e.g.

public class Question
{
    public int HitCount { get; set; }
}

You can then display this in your page.

AdamRalph
Hi Adam,But i don't want to increment the hitcount for the user that already viewed the page before.. only for new user. Does that mean i should also log the Ip adress of the user to determine if he already view the page?
happy
Logging only one hit per user is going to be more difficult and, if you are allowing anonymous access, perhaps even impossible. If your website insists on a user being logged in, then you can log only one hit per username for the page and use the method I describe above for displaying it to the user. If, however, you allow anonymous access, then the only identifer you have is the IP address. Imagine a corporation with 1,000 employees who all access the internet through a single proxy. You would effectively log only one hit even though all 1,000 people might request the page.
AdamRalph
thanks adam. thanks for the info.
happy