2004年11月16日

一个最最简单的HttpHandler和HttpModule的事例,

首先实现一个HttpHandler类
using System.Web;
using System.Collections;

namespace WhosOn
{
    
public class WhosOnHandler:IHttpHandler
    
{
        
        
public void ProcessRequest(HttpContext objContext)
        
{
            Queue colPageStats;

            colPageStats = ((Queue)objContext.Cache["whoson"]);
            
if (!(colPageStats == null)) 
            
{
                objContext.Response.Write(
"<table border=1 cellpadding=4>");
                objContext.Response.Write(
"<tr><td colspan=4 bgcolor=orange>");
                objContext.Response.Write(
"<b>Who's On</b>");
                objContext.Response.Write(
"</td></tr>");
                objContext.Response.Write(
"<tr colspan=4 bgcolor=#eeeeee>");
                objContext.Response.Write(
"<th>Timestamp</th>");
                objContext.Response.Write(
"<th>Browser Type</th>");
                objContext.Response.Write(
"<th>Remote Address</th>");
                objContext.Response.Write(
"<th>Referrer</th>");
                objContext.Response.Write(
"</td></tr>");
                
                
foreach (StatsEntry objStatsEntry in colPageStats) 
                
{
                    objContext.Response.Write(
"<tr>");
                    objContext.Response.Write(
"<td>" + objStatsEntry.TimeStamp + "&nbsp;</td>");
                    objContext.Response.Write(
"<td>" + objStatsEntry.BrowserType + "&nbsp;</td>");
                    objContext.Response.Write(
"<td>" + objStatsEntry.UserHostName + "&nbsp;</td>");
                    objContext.Response.Write(
"<td>" + objStatsEntry.Referrer + "&nbsp;</td>");
                }

                objContext.Response.Write(
"</table>");
            }

        }


        
public bool IsReusable 
        
{
            
get 
            
{
                
return true;
            }

        }

    }

}

然后再实现一个HttpModule类
using System;
using System.Web;
using System.Collections;
using System.Configuration;

namespace WhosOn
{
    
public class WhosOnModule:IHttpModule
    
{

        
public void Init(HttpApplication myApp)
        
{
            myApp.BeginRequest 
+= new EventHandler(this.OnEnter);
        }


        
public void Dispose()
        
{
        }


        
public void OnEnter(object s, EventArgs e)
        
{
            HttpApplication objApp;
            HttpContext objContext;
            
string strPath;
            Queue colPageStats;
            StatsEntry objStatsEntry;
            
int intMaxEntries;
            objApp 
= ((HttpApplication)s);
            objContext 
= objApp.Context;
            strPath 
= objContext.Request.Path.ToLower();
            if(strPath.EndsWith(".axd"))
            {
                
goto exitMethodDeclaration0;
            }

            intMaxEntries = System.Convert.ToInt32(ConfigurationSettings.AppSettings["whoson"]);
            colPageStats = ((Queue)objContext.Cache["whoson"]);
            
if (colPageStats == null
            
{
                colPageStats 
= new Queue();
            }

            objStatsEntry = new StatsEntry(objContext.Timestamp, objContext.Request.Browser.Type, objContext.Request.UserHostName, objContext.Request.ServerVariables["HTTP_REFERER"]);
            colPageStats.Enqueue(objStatsEntry);

            if (intMaxEntries != 0
            
{
                
while (colPageStats.Count > intMaxEntries) 
                
{
                    colPageStats.Dequeue();
                }

            }

            objContext.Cache["whoson"= colPageStats;
            exitMethodDeclaration0: ;
        }

    }

    
public class StatsEntry
    
{
        
public DateTime TimeStamp;
        
public string BrowserType;
        
public string UserHostName;
        
public string Referrer;

        
public StatsEntry(DateTime TimeStamp, string BrowserType, string UserHostName, string Referrer)
        
{
            
this.TimeStamp = TimeStamp;
            
this.BrowserType = BrowserType;
            
this.UserHostName = UserHostName;
            
this.Referrer = Referrer;
        }

    }

}

将编译的两个WhosOnModule和WhosOnHandler两个dll 放到一个web application的bin目录里,并在这个web application的web.config中添加如下项目
<configuration>
  
<system.web>
    
<httpModules>
    
<add name="WhosOnModule"
     type
="WhosOn.WhosOnModule,WhosOnModule" />
    
</httpModules>
    
<httpHandlers>
      
<add verb="*" path="*.axd"
      type
="WhosOn.WhosOnHandler,WhosOnHandler" />
    
</httpHandlers>
  
</system.web>
</configuration>



posted @ 2004-11-16 00:10 鲁旭 阅读(1427) 评论(2) 编辑


2004年10月28日

如题,,如果不能得话,哪位大哥推荐个比较好用的报表的控件?

posted @ 2004-10-28 14:52 鲁旭 阅读(840) 评论(1) 编辑


2004年10月19日

在学习接口继承的时候发现书上有这么个例子,看了之后晕死了,是不是以后就不用值类型了呢?
using System;

//定义Change 方法的接口
interface IChangeBoxedPoint
{
    
void Change(Int32 x,Int32 y);
}


//让Point值类型实现IChangeBoxedPoint接口
struct Point:IChangeBoxedPoint
{
    
public Int32 x,y;
    
public void Change(Int32 x,Int32 y)
    
{
        
this.x=x;
        
this.y=y;
    }


    
public override String ToString()
    
{
        
return String.Format("({0},{1})",x,y);
    }

}

class CPoint:IChangeBoxedPoint
{
    
public Int32 x,y;
    
IChangeBoxedPoint 成员
    
public override String ToString()
    
{
        
return String.Format("({0},{1})",x,y);
    }

}



class App
{
    
static void Main()
    
{
        Point p
=new Point();
        p.x
=p.y=1;
        Console.WriteLine(p);
//显示(1,1)

        p.Change(
2,2);
        Console.WriteLine(p);
//显示(2,2)

        Object o
=p;
        Console.WriteLine(o);
//显示(2,2)

        ((Point)o).Change(
3,3);
        Console.WriteLine(o);
//显示(2,2)

        
//对p执行装箱,然后改变已装箱对象,最后将之丢弃
        ((IChangeBoxedPoint)p).Change(4,4);
        Console.WriteLine(p);
//显示(2,2)

        
//改变已装箱对象,并显示其内容
        ((IChangeBoxedPoint)o).Change(5,5);
        Console.WriteLine(o);
//显示(5,5)

        
        
        CPoint cp
=new CPoint();
        cp.x
=cp.y=1;
        Console.WriteLine(cp);
//显示(1,1)

        cp.Change(
2,2);
        Console.WriteLine(cp);
//显示(2,2)

        Object co
=cp;
        Console.WriteLine(co);
//显示(2,2)

        ((CPoint)co).Change(
3,3);
        Console.WriteLine(co);
//显示(3,3)

        
//对p执行装箱,然后改变已装箱对象,最后将之丢弃
        ((IChangeBoxedPoint)cp).Change(4,4);
        Console.WriteLine(cp);
//显示(4,4)

        
//改变已装箱对象,并显示其内容
        ((IChangeBoxedPoint)co).Change(5,5);
        Console.WriteLine(co);
//显示(5,5)

        Console.ReadLine();

    }

}

posted @ 2004-10-19 14:11 鲁旭 阅读(982) 评论(3) 编辑


2004年10月18日

实现函数:

public static Array Redim(Array origArray,int newSize)
{
  
//确定每个元素的类型
  Type t=origArray.GetType().GetElementType();

  
//创建一个有期望个数的新数组
  
//新数组的类型必须和原数组的类型匹配
  Array newArray=Array.CreateInstance(t,newSize);
  
//将原数组中的元素拷贝到新数组中
  Array.Copy(origArray,0,newArray,0,Math.Min(origArray.Length,newSize));
 
//返回新数组
 return newArray;
}

posted @ 2004-10-18 22:51 鲁旭 阅读(2081) 评论(2) 编辑

   今天在看《.net 框架程序设计》的事件原理,抄了个例子:
发布事件:
using System;
class MailManager
{
    
//在mailmanager内部定义MailMsgEventArgs类型
    public class MailMsgEventArgs:EventArgs
    
{
        
public readonly String from,to,subject,body;
        
//1.传递给事件接收者的类型定义信息
        public MailMsgEventArgs(String from,String to,String subject,String body)
        
{
            
this.from=from;
            
this.to=to;
            
this.subject=subject;
            
this.body=body;
        }


    }


    
//2.下面的委托类型定义了接收者必须实现的回调方法原型
    public delegate void MailMsgEventHandler(Object sender,MailMsgEventArgs args);
    
    
//3.事件成员
    public event MailMsgEventHandler MailMsg;

    
//4.下面的受保护虚方法负责通知事件的登记对象
    protected virtual void OnMailMsg(MailMsgEventArgs e)
    
{
        
if(MailMsg != null)
        
{
            MailMsg(
this,e);
        }

    }


    
//5.下面的方法将输入转化为期望的事件,该方法在新的信息到达时被调用
    public void SimulateArrivingMsg(String from,String to,String subject,String body)
    
{
        
//构造一个对象保存希望传递给通知接收者的信息
        MailMsgEventArgs e=new MailMsgEventArgs(from,to,subject,body);
        
//调用虚方法通知对象时间已经发生
        
//如果派生类型没有重写该虚方法,对象将通知所有的登记的事件侦听者
        OnMailMsg(e);
    }



}


侦听事件:
using System;
class Fax
{
    
/// <summary>
    
///将MailManager对象传递给构造器 
    
/// </summary>
    
/// <param name="mm"></param>

    public Fax(MailManager mm)
    
{
        
//构造一个指向FaxMsg回调方法的MailMsgEventHandler
        
//委托实例。然后登记MailManager的MailMsg事件
        mm.MailMsg+=new MailManager.MailMsgEventHandler(FaxMsg);
    }


    
/// <summary>
    
/// MailManager将调用该方法来通知Fax对象接收到一个新的电子邮件信息
    
/// </summary>
    
/// <param name="sender">表示MailManager对象,如果期望和事件的触发着通信,将会用到该参数</param>
    
/// <param name="e">表示MailManager对象希望提供的一些附加事件信息</param>

    private void FaxMsg(Object sender,MailManager.MailMsgEventArgs e)
    
{
        Console.WriteLine(
"Faxing mail message:");
        Console.WriteLine(
"From :{0}\n To:{1}\n Subject:{2}\n Body:{3}\n",e.from,e.to,e.subject,e.body);
    }

    
/// <summary>
    
/// 注销事件
    
/// </summary>
    
/// <param name="mm"></param>

    public void Unregister(MailManager mm)
    
{
        
//构造一个指向FaxMsg回调方法的MailMsgEventHandler委托实例
        MailManager.MailMsgEventHandler callback=new MailManager.MailMsgEventHandler(FaxMsg);
        
//注销MailManager的MailMsg事件
        mm.MailMsg-=callback;
    }

}

posted @ 2004-10-18 22:40 鲁旭 阅读(1286) 评论(3) 编辑


2004年9月23日

    public class MainTest
    
{
        
public static void Main()
        
{
            BaseMyClass baseMy
=new BaseMyClass();
            IntMyClass IntMy
=new IntMyClass();
            BaseMyClass IntMy2
=new IntMyClass();
            
            baseMy.ShowMessage1();
            baseMy.ShowMessage2();

            IntMy.ShowMessage1();
            IntMy.ShowMessage2();

            IntMy2.ShowMessage1();
            IntMy2.ShowMessage2();

            Console.ReadLine();

        }

    }

    
public class BaseMyClass
    
{
        
public void ShowMessage1()
        
{
            Console.WriteLine(
"Base1");
        }

        
public virtual void ShowMessage2()
        
{
            Console.WriteLine(
"Base2");
        }


    }

    
    
public class IntMyClass:BaseMyClass
    
{
        
public new void ShowMessage1()
        
{
            Console.WriteLine(
"IntMy1");
        }

        
public override void ShowMessage2()
        
{
            Console.WriteLine(
"IntMy2");
        }


    }
以上代码执行的结果为:
Base1
Base2
IntMy1
IntMy2
Base1
IntMy2

如果把IntMyClass中函数ShowMessage1的new去掉后执行的结果
Base1
Base2
IntMy1
IntMy2
Base1
IntMy2
没有区别

如果将IntMyClass中函数ShowMessage2的override去掉执行的结果为
Base1
Base2
IntMy1
IntMy2
Base1
Base2
:)


posted @ 2004-09-23 22:43 鲁旭 阅读(783) 评论(0) 编辑


2004年9月22日

摘要: usingSystem;namespaceArrayTest{/**////<summary>///test的摘要说明。///</summary>publicclasstest{publicstaticvoidMain(){myArray[]myArrayTest=newmyArray[6];for(inti=0;i<6;i++)Console.WriteLine("...阅读全文

posted @ 2004-09-22 21:23 鲁旭 阅读(744) 评论(1) 编辑


2004年9月12日

摘要: 哪里能下载到《 Microsoft .NET框架程序设计(修订版) 》电子版?谢谢阅读全文

posted @ 2004-09-12 17:27 鲁旭 阅读(5652) 评论(12) 编辑


2004年9月7日

摘要: 今天下载了个rainbow,数据库已经安装成功。但访问default.aspx页时 老是出错。 Server Error in '/Rainbow' Application. Error executing child request for DesktopDefault.aspx. Description: An unhandled exception occurred during the e...阅读全文

posted @ 2004-09-07 23:42 鲁旭 阅读(492) 评论(1) 编辑


2004年9月6日

摘要: 昨天才发现这么一个好地方,大概浏览了一下各个大虾的文章,才发现自己是比较落伍的。一定要好好努力了阅读全文

posted @ 2004-09-06 10:13 鲁旭 阅读(353) 评论(0) 编辑


posts - 10, comments - 25, trackbacks - 0, articles - 1

Copyright © 鲁旭