如何在ASP.NET MVC中创建旋转广告块?

时间:2009-02-19 02:15:29

标签: asp.net-mvc

如何使用ASP.NET MVC实现每个页面刷新的旋转广告块,类似于SO,

您认为他们的会话变量包含我们当前正在查看的广告索引,并将每个请求或其他内容循环播放吗?

5 个答案:

答案 0 :(得分:16)

我会使用OpenXDARTGoogle Ad ManagerRightMediaRubicon或其他广告管理员。

但是,如果我想把它自己建成一个练习,我会:

  1. 创建广告数据库表
  2. 在我的基本控制器中,选择随机广告到ViewData
  3. 将部分视图添加到母版页以呈现广告
  4. [最重要的]使用Phil Haack的MVC风格版“donut caching
  5. 这与您用于显示循环报价单,随机特色用户或您希望在每个页面上显示的任何其他随机内容的方法相同。

答案 1 :(得分:3)

我不知道MVC,但有什么阻止你使用AdRotator控件吗?

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.adrotator.aspx

答案 2 :(得分:1)

您可以生成一个随机数,并将其用作您要显示的广告的数组键,但是您无法确保均匀曝光,并且在兑换货币时会产生许多问题。为此目的而构建的应用是OpenX。这提供了非常深入的统计数据,它还允许您设置每个广告需要显示的数量等。它非常强大,许多商业网站都使用它来管理他们的广告。

答案 3 :(得分:0)

我必须在MVC中为加权广告控件写一些内容,如果可能的话,连续两次没有显示相同的广告,并且在我的忙碌中我最终得到了一些可能会为您带来灵感的可怕代码。

我确信有很多更好的方法可以做到这一点(而且我已经知道在不应该这样做时可以复制的情况)但是我花了很短的时间就完成了这项工作。

    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;
    }

答案 4 :(得分:0)

以下博客将为Ad Rotator的实施做好准备。

1)以下是在ASP.NET中,但仍然完全基于Jquery和简单。

http://dongavipul.blogspot.in/2011/02/jquery-adrotator-like-aspnet-adrotator.html

2)以下给出了ad rotator的Helper方法实现。

http://weblogs.asp.net/rashid/archive/2009/04/20/adrotator-for-asp-net-mvc.aspx