views:

864

answers:

4

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?

+1  A: 

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.

Sam152
If you refresh Stackoverflow many times, you see they cycle in the same order each refresh.
KingNestor
+1 - I like the idea of OpenX, but I'd be interested in seeing if there is a .net version of something similar :-)
WestDiscGolf
OpenX is great because it manages everything for you! No extra load on your server, really powerful stats, allows you to manage how much each advert is shown and when ect. You don't actually install it on your server, they host everything.
Sam152
+2  A: 

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

Iain M Norman
+13  A: 

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:

  1. Create a database table of advertisements
  2. In my base controller, select a random advertisement into the ViewData
  3. Add a partial view to your master page to render the ad
  4. [Most importantly] Use Phil Haack's MVC-style version of "donut caching"

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.

Portman
A: 

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;
    }
Graphain
Just noticed that you want the same cycle each time. You should clarify the behaviour in your post. I'll see if I have some code I can post.
Graphain
I'd just follow Portman's answer [http://stackoverflow.com/questions/563706/how-do-i-create-rotating-ad-blocks-in-asp-net-mvc-like-stackoverflow-does/586052#586052] and use cookies or session variables as suggested for maintaining position.
Graphain