日期:2014-01-28  浏览次数:20980 次

 

仿照msdn写了一个接管http请求的类,对于aspx文件,可以正常实现功能,但是对于图片文件,比如
*.jpg却不行,找了一些资料也没搞清楚,cs文件和config文件如下:

搞定了,在IIS中指定对.jpg文件的映射

[D:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll]

HandlerFactory.cs

using System;
using System.Web;

namespace Handlers
{
 class HandlerFactory : IHttpHandlerFactory
 {
  public IHttpHandler GetHandler(HttpContext context, string requestType, String url, String pathTranslated)
  {        
   IHttpHandler handlerToReturn;
   if("get" == context.Request.RequestType.ToLower())
   {
    handlerToReturn = new HandlerImg();
   }
   else if("post" == context.Request.RequestType.ToLower())
   {
    handlerToReturn = new HandlerImg();
   }
   else
   {
    handlerToReturn = null;
   }
   return handlerToReturn;
  }
  public void ReleaseHandler(IHttpHandler handler)
  {
  }
  public bool IsReusable
  {
   get
   {
    return false;
   }
  }
 }
 public class HandlerImg : IHttpHandler
 {
  public virtual void ProcessRequest(HttpContext context)
  {
   context.Response.Redirect("http://hr.benq.com");

  }
   
  public virtual bool IsReusable
  {
   get { return true; }
  }
 }
}

web.config

<httpHandlers>
         <add verb="*" path="test1.aspx" type="Handlers.HandlerFactory,Handlers" />
         <add verb="*" path="test2.aspx" type="Handlers.HandlerFactory,Handlers" />
         <add verb="*" path="*.jpg" type="Handlers.HandlerFactory,Handlers" />
   </httpHandlers>