Programming an In-Text Advertising System under ASP.NET 3.5 - The Three Generic Handlers Related Programming
(Page 4 of 4 )
Now, we’ll check out the three generic handlers one by one.
AdArray.ashx
The first handler, AdArray.ashx, is used to generate the ad data related array to be used on the client side:
public class adArray : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";//the output type
string result="var Ads=new Array(";//specify the script variable to be returned
Sql s =new Sql();
System.Data .DataSet ds=s.getMyDataSet("select name from keyarray ");//obtain the ad keyword
for(int i=0;i<ds.Tables[0].Rows.Count;i++)//append the obtained keyword to the string
result += "'" + ds.Tables[0].Rows[i][0].ToString() + "',";
if (result.EndsWith(","))//clear out the unwanted ',' character at the end of the string
result = result.Substring(0,result.Length-1);
result +=");";
context.Response.Write(result);//output the script variable
}
public bool IsReusable {//not reusable
get {
return false;
}
}
}
As you’ve seen, the main function of the adArray handler searches an advertisement key word from the database, then generates an advertisement key word array according to the specified format, and finally returns the data to the client side. Next, let’s check out the second handler, AD.ashx.
AD.ashx
The second handler, AD.ashx, will render the concrete ad contents related to some special ad keyword. In detail, it will query the corresponding ad contents from the tableaddefined in the AD.mdf database, and then return the data to the client side with some special format, which will finally be rendered on the client side. Below I've listed the associated code for the AD.ashx handler:
public class AD : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string result = "";//the returned ad content
Sql s = new Sql();
string key = HttpContext.Current.Request.QueryString["key"];//the specified ad keyword
string username = HttpContext.Current.Request.QueryString["username"];// the account of the website host who places the ad
string str = "select description ,url from ad inner join keyarray on ad.keyid=keyarray.id where keyarray.name='"+key+"' order by ad.id desc";
System.Data.DataSet ds = s.getMyDataSet(str);//obtain the ad content
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)//append all the relevant ad content to the string
{
result += "<a target="_blank" href="Redirect.ashx?username=" + username;
result += "&url=" + ds.Tables[0].Rows[i]["url"].ToString() + "">" + ds.Tables[0].Rows[i]["description"].ToString() + "</a><br/>";
}
context.Response.Write(result);//output
}
public bool IsReusable {//not reusable
get {
return false;
}
}
}
Finally, let’s look at the third and last handler—redirect.ashx. As the name hints, this handler sees to record the hit count of the related ad, and also bears the responsibility of redirecting the current user to the website where the real ad-targeted contents exists. The following is the related code for the redirect.ashx handler:
public class redirect : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string username = HttpContext.Current.Request.QueryString["username"];//the account of the website host who places the ad
string url = HttpContext.Current.Request.QueryString["url"];//the url to redirect
Sql s = new Sql();
s.ExecuteSql("insert into hit(username,url)values('"+username+"','"+url +"')");//insert the ad data
HttpContext.Current.Response.Redirect(url);//navigate the ad related website page
}
public bool IsReusable {//non-reusable
get {
return false;
}
}
}
-DOWNLOAD SOURCE-
Summary
In this installment, we mainly focused upon the core code programming of the in-text ad system. As you’ve seen, the real work is around the client side JavaScript programming. To gain a better understanding of this, you should first comprehend the invoking relationships between the script files. Then, we explored parts of the server side programming, such as the password encrypting, basic database operation encapsulation, and all three higher generic handlers, which are mainly leveraged to process and provide ad-related data to the client side scripts.
In the third and last part of the in-text advertising system, we’ll shift to discussing the entire website owner- and administrator-related server-side programming and see how the items we discussed in this part will fit together. Please stay tuned for the final installment.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |