httphandler在网上一搜到处都是,可是要是能变成自己的httphandler需要理解了才行。比如要在网页上显示图片,可以用httphandler来实现,使加快相应速度。不去处理那些不必要的操作。
可以看看这篇文章
http://www.cnblogs.com/HeroBeast/archive/2008/02/13/1067857.html
其实图片在web里也是通过httphandler来处理的 ,如下
1 public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
2 {
3 string imgType = context.Request.QueryString["ext"];
4 if(string.IsNullOrEmpty(imgType))
5 imgType = "jpg";
6 imgType = imgType.ToLower();
7
8 if (imgType == "jpg")
9 return new JpgHandler();
10 else
11 return new GifHandler();
12 }
2 {
3 string imgType = context.Request.QueryString["ext"];
4 if(string.IsNullOrEmpty(imgType))
5 imgType = "jpg";
6 imgType = imgType.ToLower();
7
8 if (imgType == "jpg")
9 return new JpgHandler();
10 else
11 return new GifHandler();
12 }
这是通过IHttpHandlerFactory来实现的。
但是时候是想对aspx页面进行处理,要是web.config是这样写的
1 <httpHandlers>
2 <add verb="*" path="*" type="BaseHttpHandlerFactory.BaseHttpHandlerFactory,BaseHttpHandlerFactory"/>
3 </httpHandlers>
4
2 <add verb="*" path="*" type="BaseHttpHandlerFactory.BaseHttpHandlerFactory,BaseHttpHandlerFactory"/>
3 </httpHandlers>
4
也是用工厂模式的httphandler实现的其实我只想对两个页面进行处理,HttpHandler1.aspx和HttpHandler2.aspx,其余的aspx我还想用系统默认的处理该怎么做呢?看下面的
1 public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
2 {
3 string strHttpHandler = url.ToString();
4
5 if (strHttpHandler.Contains("HttpHandler1.aspx"))
6 {
7 return new HttpHandler1();
8 }
9 else if (strHttpHandler.Contains("HttpHandler2.aspx"))
10 {
11 return new HttpHandler2();
12 }
13 else
14 {
15 //系统默认处理
16 return PageParser.GetCompiledPageInstance(url, pathTranslated, context);
17 }
18
19
20 }
看到第16行了吧,这样就可以实现HttpHandler1.aspx用httphandler1进行处理和HttpHandler2.aspx用httphandler2进行处理了,其余的用page默认的httphandler进行处理。2 {
3 string strHttpHandler = url.ToString();
4
5 if (strHttpHandler.Contains("HttpHandler1.aspx"))
6 {
7 return new HttpHandler1();
8 }
9 else if (strHttpHandler.Contains("HttpHandler2.aspx"))
10 {
11 return new HttpHandler2();
12 }
13 else
14 {
15 //系统默认处理
16 return PageParser.GetCompiledPageInstance(url, pathTranslated, context);
17 }
18
19
20 }
下载源代码:/Files/HeroBeast/HttpHandlerSolution.rar