以下是我这几天这个项目中添加的广告功能,算是新的尝试,,由于上班缘故就没那么清楚了,
仅贴下代码,呵呵代码质量不是很好,应付完工的,希望多提意见!
广告相关信息如图:
此采用的分页使用了我上篇随笔的分页控件。。。
绑定广告代码
a.如果显示一个广告位下的一个广告:
<script>$viewAd('标识')</script>
b.显示一个广告位下的多个广告
<script>$viewAd('标识',广告个数);</script>
如:<script>$viewAd('index_top',10);</script>
<script>$viewAd('标识')</script>
b.显示一个广告位下的多个广告
<script>$viewAd('标识',广告个数);</script>
如:<script>$viewAd('index_top',10);</script>
怎样简单吧。。。
下面是广告代码的生成
1.AdType.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WgEdu.Models.Ad
{
public enum AdType
{
Text,Image
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WgEdu.Models.Ad
{
public enum AdType
{
Text,Image
}
}
2.AdBase.cs
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq.Mapping;
using WgEdu.Models;
using WgEdu.Models.Repository;
namespace WgEdu.Models.Ad
{
[Table(Name = "wgedu_ads")]
public partial class AdBase : Interface.IAd
{
[Column(DbType="bigint",IsPrimaryKey = true,IsDbGenerated=true)]
public int ID
{
get;
set;
}
[Column]
public string Mark
{
get;
set;
}
[Column(Name="word")]
public string Text
{
get;
set;
}
[Column(Name="imgurl")]
public string ImageUrl { get; set; }
[Column]
public string LinkUrl
{
get;
set;
}
[Column]
public bool IsUsed { get; set; }
[Column(DbType = "datetime", IsDbGenerated = true)]
public DateTime AddDate
{
get;
set;
}
[Column(DbType = "datetime", UpdateCheck = UpdateCheck.Never)]
public DateTime EndDate
{
get;
set;
}
}
public partial class AdBase
{
public string Type
{
get
{
var s= from c in new AdPositionRepository().Table
where c.Mark == Mark
select new { c.Type };
return s.SingleOrDefault().Type;
}
}
public static void Add(AdBase ad)
{
var rp = new AdRepository();
rp.Add(ad);
rp.Save();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq.Mapping;
using WgEdu.Models;
using WgEdu.Models.Repository;
namespace WgEdu.Models.Ad
{
[Table(Name = "wgedu_ads")]
public partial class AdBase : Interface.IAd
{
[Column(DbType="bigint",IsPrimaryKey = true,IsDbGenerated=true)]
public int ID
{
get;
set;
}
[Column]
public string Mark
{
get;
set;
}
[Column(Name="word")]
public string Text
{
get;
set;
}
[Column(Name="imgurl")]
public string ImageUrl { get; set; }
[Column]
public string LinkUrl
{
get;
set;
}
[Column]
public bool IsUsed { get; set; }
[Column(DbType = "datetime", IsDbGenerated = true)]
public DateTime AddDate
{
get;
set;
}
[Column(DbType = "datetime", UpdateCheck = UpdateCheck.Never)]
public DateTime EndDate
{
get;
set;
}
}
public partial class AdBase
{
public string Type
{
get
{
var s= from c in new AdPositionRepository().Table
where c.Mark == Mark
select new { c.Type };
return s.SingleOrDefault().Type;
}
}
public static void Add(AdBase ad)
{
var rp = new AdRepository();
rp.Add(ad);
rp.Save();
}
}
}
3.adposition.cs 广告位置
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq.Mapping;
using WgEdu.Models.Repository;
using WgEdu.Models;
using System.Text;
namespace WgEdu.Models.Ad
{
/// <summary>
/// 广告位
/// </summary>
[Table(Name="wgedu_adposition")]
public partial class AdPosition
{
[Column(DbType = "bigint", IsPrimaryKey = true, IsDbGenerated = true)]
public int ID {get;set;}
[Column]
public string Mark { get;set; }
[Column]
public string Type { get; set; }
[Column]
public string Description { get; set; }
}
public partial class AdPosition
{
public AdPosition()
{
}
public AdPosition(string mark)
{
this.Mark = mark;
var ap = new AdPositionRepository().Get(a => a.Mark == mark);
if (ap != null)
{
this.Type = ap.Type;
}
}
public static void Add(AdPosition adpos)
{
var rp = new AdPositionRepository();
rp.Add(adpos);
rp.Save();
}
/// <summary>
/// 获得该广告位下的单个广告
/// </summary>
/// <returns></returns>
public string GetSingleAdHtmlContent()
{
StringBuilder sb = new StringBuilder();
var ad = new AdRepository().Get(a => a.Mark == this.Mark&&DateTime.Now<=a.EndDate&&a.IsUsed);
if (this.Type == AdType.Text.ToString().ToLower())
{
if (ad == null)
{
sb.Append("<a href=\"/ads/freead?mark="+this.Mark+"\" target=\"_blank\">广告位招租中</a>");
}
sb.Append("<a href=\"" + ad.LinkUrl + "\" target=\"_blank\">" + ad.Text + "</a>");
}
else if (this.Type == AdType.Image.ToString().ToLower())
{
if (ad == null)
{
sb.Append("<A href=\"/ads/freead?mark="+this.Mark+"\" target=\"_black\">" +
"<img src=\"/ads/images/picad_zz.gif\" class=\"adimg\" border=\"0\"/></a>");
}
else
{
sb.Append("<a href=\"" + ad.LinkUrl + "\" target=\"_black\"><img src=\"" +
ad.ImageUrl + "\" class=\"adimg\" border=\"0\"/></a>");
}
}
return sb.ToString();
}
/// <summary>
/// 获取该广告位下的所有广告以li标签显示
/// </summary>
/// <param name="num">要加载的总数,如果为空则加载所有</param>
/// <returns></returns>
public string GetListAdHtmlContent(int? num)
{
StringBuilder sb = new StringBuilder();
var items = new AdRepository().
Where(a => a.Mark == Mark && a.EndDate >= DateTime.Now&&a.IsUsed)
.Take(num ?? 100000);
if (this.Type == AdType.Text.ToString().ToLower())
{
if (items.Count() == 0)
{
sb.Append("<li><a href=\"/ads/freead?mark=" + this.Mark + "\" target=\"_blank\">广告位招租中</a></li>");
}
else
{
foreach (var item in items)
{
sb.Append("<li><a href=\"" + item.LinkUrl + "\" target=\"_blank\">" +
item.Text + "</a></li>");
}
}
}
else if (this.Type == AdType.Image.ToString().ToLower())
{
if (items.Count() == 0)
{
sb.Append("<li><a href=\"/ads/freead?mark=" + this.Mark + "\" target=\"_black\">" +
"<img src=\"/ads/images/picad_zz.gif\" class=\"adimg\" border=\"0\"/></a></li>");
}
else
{
foreach (var item in items)
{
sb.Append("<li><a href=\"" + item.LinkUrl + "\" target=\"_blank\">" +
"<img src=\"" + item.ImageUrl + "\" class=\"adimg\" border=\"0\"/></a></li>");
}
}
}
return sb.ToString();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq.Mapping;
using WgEdu.Models.Repository;
using WgEdu.Models;
using System.Text;
namespace WgEdu.Models.Ad
{
/// <summary>
/// 广告位
/// </summary>
[Table(Name="wgedu_adposition")]
public partial class AdPosition
{
[Column(DbType = "bigint", IsPrimaryKey = true, IsDbGenerated = true)]
public int ID {get;set;}
[Column]
public string Mark { get;set; }
[Column]
public string Type { get; set; }
[Column]
public string Description { get; set; }
}
public partial class AdPosition
{
public AdPosition()
{
}
public AdPosition(string mark)
{
this.Mark = mark;
var ap = new AdPositionRepository().Get(a => a.Mark == mark);
if (ap != null)
{
this.Type = ap.Type;
}
}
public static void Add(AdPosition adpos)
{
var rp = new AdPositionRepository();
rp.Add(adpos);
rp.Save();
}
/// <summary>
/// 获得该广告位下的单个广告
/// </summary>
/// <returns></returns>
public string GetSingleAdHtmlContent()
{
StringBuilder sb = new StringBuilder();
var ad = new AdRepository().Get(a => a.Mark == this.Mark&&DateTime.Now<=a.EndDate&&a.IsUsed);
if (this.Type == AdType.Text.ToString().ToLower())
{
if (ad == null)
{
sb.Append("<a href=\"/ads/freead?mark="+this.Mark+"\" target=\"_blank\">广告位招租中</a>");
}
sb.Append("<a href=\"" + ad.LinkUrl + "\" target=\"_blank\">" + ad.Text + "</a>");
}
else if (this.Type == AdType.Image.ToString().ToLower())
{
if (ad == null)
{
sb.Append("<A href=\"/ads/freead?mark="+this.Mark+"\" target=\"_black\">" +
"<img src=\"/ads/images/picad_zz.gif\" class=\"adimg\" border=\"0\"/></a>");
}
else
{
sb.Append("<a href=\"" + ad.LinkUrl + "\" target=\"_black\"><img src=\"" +
ad.ImageUrl + "\" class=\"adimg\" border=\"0\"/></a>");
}
}
return sb.ToString();
}
/// <summary>
/// 获取该广告位下的所有广告以li标签显示
/// </summary>
/// <param name="num">要加载的总数,如果为空则加载所有</param>
/// <returns></returns>
public string GetListAdHtmlContent(int? num)
{
StringBuilder sb = new StringBuilder();
var items = new AdRepository().
Where(a => a.Mark == Mark && a.EndDate >= DateTime.Now&&a.IsUsed)
.Take(num ?? 100000);
if (this.Type == AdType.Text.ToString().ToLower())
{
if (items.Count() == 0)
{
sb.Append("<li><a href=\"/ads/freead?mark=" + this.Mark + "\" target=\"_blank\">广告位招租中</a></li>");
}
else
{
foreach (var item in items)
{
sb.Append("<li><a href=\"" + item.LinkUrl + "\" target=\"_blank\">" +
item.Text + "</a></li>");
}
}
}
else if (this.Type == AdType.Image.ToString().ToLower())
{
if (items.Count() == 0)
{
sb.Append("<li><a href=\"/ads/freead?mark=" + this.Mark + "\" target=\"_black\">" +
"<img src=\"/ads/images/picad_zz.gif\" class=\"adimg\" border=\"0\"/></a></li>");
}
else
{
foreach (var item in items)
{
sb.Append("<li><a href=\"" + item.LinkUrl + "\" target=\"_blank\">" +
"<img src=\"" + item.ImageUrl + "\" class=\"adimg\" border=\"0\"/></a></li>");
}
}
}
return sb.ToString();
}
}
}
下面是生成javascript可调用的广告代码:
AdsController.cs
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using WgEdu.Models;
using WgEdu.Models.Ad;
using WgEdu.Models.Repository;
namespace WgEdu.Controllers
{
public class AdsController : Controller
{
//
// GET: /Ads/
// id====作为mark使用
public ActionResult Index()
{
return View();
}
public ActionResult FreeAd(string mark)
{
return View();
}
public JavaScriptResult SingleAd(string id)
{
return JavaScript("document.write('"+new AdPosition(id).GetSingleAdHtmlContent()+"');");
}
public JavaScriptResult ListAd(string id, int? num)
{
return JavaScript("document.write('"+new AdPosition(id).GetListAdHtmlContent(num)+"');");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using WgEdu.Models;
using WgEdu.Models.Ad;
using WgEdu.Models.Repository;
namespace WgEdu.Controllers
{
public class AdsController : Controller
{
//
// GET: /Ads/
// id====作为mark使用
public ActionResult Index()
{
return View();
}
public ActionResult FreeAd(string mark)
{
return View();
}
public JavaScriptResult SingleAd(string id)
{
return JavaScript("document.write('"+new AdPosition(id).GetSingleAdHtmlContent()+"');");
}
public JavaScriptResult ListAd(string id, int? num)
{
return JavaScript("document.write('"+new AdPosition(id).GetListAdHtmlContent(num)+"');");
}
}
}
创建Js调用Javascript代码:
Code
//获取url参数
function $getUriParameter(obj) {
var uri = location.href;
var ps = uri.substring(uri.indexOf(obj + "=") +(obj.toString().length+1));
if (ps.indexOf("&")==-1) {return ps;}
else {return ps.substring(0, ps.indexOf("&")); }
}
//调用广告
function $viewAd(mark, num) {
var jsUri = "/ads/" + (num == null ? "singleAd/" + mark : "ListAd/" + mark + "?num=" + num);
document.write("<script type='text/javascript' src='" + jsUri + "'></" + "script>");
}
//获取url参数
function $getUriParameter(obj) {
var uri = location.href;
var ps = uri.substring(uri.indexOf(obj + "=") +(obj.toString().length+1));
if (ps.indexOf("&")==-1) {return ps;}
else {return ps.substring(0, ps.indexOf("&")); }
}
//调用广告
function $viewAd(mark, num) {
var jsUri = "/ads/" + (num == null ? "singleAd/" + mark : "ListAd/" + mark + "?num=" + num);
document.write("<script type='text/javascript' src='" + jsUri + "'></" + "script>");
}
这样在页面中就可以使用上面的绑定代码进行绑定了。。
我们还加了freead Action表示广告位招租。。。。。
还有就是广告的维护代码这里就不详细写了,有时间整理一份code贴出来。。
贴我的网站的地方:http://www.rsion.com/