崬城衞 发表于 2015-8-17 11:19:08

IIS 6 通配符应用映射和HttpHandler配置

我们在对自定义HttpHandler处理时,如果需要处理某一个未设置的扩展名时,需要在IIS的摄制中,添加这一扩展名和可执行程序(ASPNET_ISAPI.Dll)的映射。不过与IIS 5 不同,在IIS 6中不允许设置*,也就是说你必须指定一个特定的扩展名,而不是一个通配符。

如果需要设置通配符映射那么要设置WildCard Application Maps。可以将ASPNET_ISAPI.Dll添加到列表中,这样你就可以通过设置Web.Config来实现对所有扩展名(包括目录)的自定义HttpHandler处理了。不过需要注意:如果这些扩展名的文件不存在,那么不要选中 "Verify that files exists"。

现在做的配置仅仅是将所有的请求都交给ASPNET_ISAPI.Dll处理,包括目录访问、静态文件等等,    性能自然就有所降低了。

http://blog.joycode.com/images/blog.joycode.com/hopeq/939/r_1.JPG

http://blog.joycode.com/images/blog.joycode.com/hopeq/939/r_2.JPG

http://blog.joycode.com/images/blog.joycode.com/hopeq/939/r_3.JPG

在Machine.Config中配置了所有的HttpHandler,也就是说,即使我们设置了通配符应用映射但没有做任何处理,那么就使用Machine.Config中的配置进行处理。

Machine.Config中的配置:
  (太长,所以这个删了,自己去看)


从Machine.Config中的配置中可以看出它对所有的内容都作了处理,如path="*"verb="GET,HEAD"的内容的功能是不在上面添加的处理规则之中的扩展名都使用System.Web.StaticFileHandler处理,即作为静态内容处理,jpg,gif,swf等等都是使用StaticFileHandler 处理的。

反编译StaticFileHandler 可以看到:
  



internal class StaticFileHandler : IHttpHandler{      // Methods      internal StaticFileHandler();      private void BuildFileItemResponse(HttpContext context, string fileName, long fileSize, DateTime lastModifiedTime, string strETag);      private void CacheValidateHandler(HttpContext context, object data, ref HttpValidationStatus validationStatus);      private static bool CompareETags(string strETag1, string strETag2);      internal static string GenerateETag(HttpContext context, DateTime lastModTime);      internal static bool IsSecurityError(int ErrorCode);      public void ProcessRequest(HttpContext context);      internal static bool SendEntireEntity(HttpContext context, string strETag, DateTime lastModifiedTime);      // Properties      public bool IsReusable { get; }      // Fields      private const int DEFAULT_CACHE_THRESHOLD = 0x40000;      private const int ERROR_ACCESS_DENIED = 5;

}




其代码内,基本都是对各种异常情况的处理,和对静态文件的读取并Response。

   需要注意:.net 2.0 beta 2(v2.0.50215)对HttpHandler的处理放在了Web.Config目录下,并更改了部分的HttpHandler,如System.Web.StaticFileHandler变成了System.Web.DefaultHttpHandler。

通过学习和模仿machine.config中HttpHandler的配置,我们可以书写我们自己的web.config配置来实现更自由的设置。

Web.Config中配置:


  





   

         

         

         

            

   httpHandlers>


要注意:不希望被path="*" 处理的扩展名需要写在上面,path="*" 写在最下面,这样就保证aspx的扩展名仍然使用System.Web.UI.Page处理,jpg,gif人仍然使用System.Web.StaticFileHandler处理。而所有除此之外的扩展名和目录访问就全部进入我们的HandlerSample.MyHandler处理。



对于path="*"配置要注意,如果不写path="*"的配置,那么就会使用上一级的设置(最终是machine.config),那么你可以只处理如path="*.a"的请求,其他的使用machine.config的配置,一旦写了path="*"的配置,那么你就要自己处理全部的扩展名的顺序。
页: [1]
查看完整版本: IIS 6 通配符应用映射和HttpHandler配置