[ httpHandlers ]利用httpHandlers来做数据统计

第一步:创建文件 UrlRewriter.cs,代码如下:
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

/// <summary>
/// UrlRewriter URL重写类
/// </summary>
public class UrlRewriter : IHttpHandler //实现“IHttpHandler”接口
{
public UrlRewriter()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void ProcessRequest(HttpContext Context)
{
LogWriter(Context.Request.RawUrl);
LogWriter(DateTime.Now.ToString()
+ "-------------------------------------");
try
{
//取得原始URL屏蔽掉参数
string Url = Context.Request.RawUrl;

string Urlsub = Url.Substring(Url.LastIndexOf("/") + 1, 2);
if (Urlsub == "207d10fg02p0229a11cv31j144")
{

String RealPath
= @"~/logo.gif";

LogWriter(RealPath);
LogWriter(DateTime.Now.ToString()
+ "**********************************");
Context.Server.Execute(RealPath);
}
else
{
String RealPath
= @"~/logo.gif";

LogWriter(RealPath);
LogWriter(DateTime.Now.ToString()
+ "-------************----------------");
Context.Server.Execute(RealPath);
//Context.Response.Redirect(Context.Request.Url.ToString());
}
}
catch
{


Context.Response.Redirect(Context.Request.Url.ToString());
}
}
/// <summary>
/// 实现“IHttpHandler”接口所必须的成员
/// </summary>
/// <value></value>
public bool IsReusable
{
get { return false; }
}

public static void LogWriter(string logstring) //记录日志-------------------
{
string LogName = null;

LogName
+= System.Web.HttpContext.Current.Server.MapPath("log") + "\\";

if (!Directory.Exists(LogName))
{
Directory.CreateDirectory(LogName);
}

LogName
+= System.DateTime.Now.Year.ToString(); ;

LogName
+= System.DateTime.Now.Month.ToString();

LogName
+= System.DateTime.Now.Day.ToString();

LogName
+= System.DateTime.Now.Hour.ToString();

LogName
+= ".log";

if (!File.Exists(LogName))
{
StreamWriter sr
= File.CreateText(LogName);
sr.WriteLine(logstring);
sr.Close();
}
else
{
StreamWriter sr
= File.AppendText(LogName);
sr.WriteLine(logstring);
sr.Close();
}
}
}

第二步 在Web.Config 中注册这个Handler。
<httpHandlers>
   <add verb="*" path="*/*.jpg,*.ppt,*/*.pp,*.aspx,*.pp,*.jpg" type="UrlRewriter"/>
  </httpHandlers>
 </system.web>

第三步:在IIS配置

具体步骤:打开IIS管理器右键点击某个网站,选择“属性”,点击到“主目录”选项卡,点击“应用程序设置”里面的“配置”按扭,打开“应用程序配置”对话框。选择其中的.jpg,点击“编辑”,把“确认文件是否存在”前面的勾去掉

c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll

第四步:把相应的链接加入,再写类实现一定的统计功能

posted @ 2011-06-27 22:40  我思故我在...  阅读(189)  评论(0编辑  收藏  举报