How would you implement a rotating ad-block with each page refresh, similar to what SO does, using ASP.NET MVC?
Do you think they have a session variable with what ad index we are currently viewing and cycle it each request or something else?
How would you implement a rotating ad-block with each page refresh, similar to what SO does, using ASP.NET MVC?
Do you think they have a session variable with what ad index we are currently viewing and cycle it each request or something else?
You could just generate a random number and use it as an array key for the advert you want to display but you wouldn't be able to ensure even exposure and it would cause many problems when it comes to the exchange of money. An app that was built for this purpose is OpenX. This provides really in depth stats and it also allows you to set how much each advert needs to be displayed ect. Its very powerful, many comercial websites employ it to manage their advertising.
I don't know MVC but is there anything stopping you using the AdRotator control?
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.adrotator.aspx
I would use OpenX, DART, Google Ad Manager, RightMedia, Rubicon, or some other ad manager.
However, if I wanted to build it myself as an exercise, I would:
This is the same approach you would use to display a cycled quotation, a randomly featured user, or any other random content that you want to display on every page.
I had to write something in MVC for a weighted ad control that didn't show the same ad twice in a row if possible and in my haste I ended up with a terrible bit of code that might serve as inspiration for you.
I'm sure there's many better ways to do this (and I already know cases where this lets duplicates through when it shouldn't) but for the short amount of time I spent on it it did the job.
public List<Ad> GetRandomWeightedAds()
{
/* Generate random order list of ads with duplicates for ViewsPerRotation */
List<Ad> returnList = GetAllAds().SelectMany(s => Enumerable.Repeat(s, s.ViewsPerRotation)).OrderBy(s => Guid.NewGuid()).ToList();
for (int i = 0; i < returnList.Count - 1; i++) /* Compare all but the last element against subsequent element */
{
if (returnList[i].Id == returnList[i + 1].Id)
{
/* If next to an identical element try and find a new spot for the subsequent element */
for (int j = 0; j < returnList.Count; j++)
{
if (returnList[j].Id != returnList[i].Id /* Don't switch identical element back into same pos*/
&& (j<i || j == 0 || j-1 == i || returnList[i].Id != returnList[j - 1].Id) /* When moving before current 'i', don't move into a place after an identical element */
&& (j<i || j == returnList.Count - 1 || j + 1 == i || returnList[i].Id != returnList[j + 1].Id)) /* When moving before current 'i', don't move into a place before an identical element */
{
returnList[i] = returnList[j];
returnList[j] = returnList[i+1]; /* returnList[i+1] == returnList[i] */
break;
}
}
}
}
return returnList;
}