仅当文件不存在时才会触发HttpHandler

我正在尝试创建一个HTTP处理程序来处理对文件夹的所有请求,但我只想在请求的文件不存在时触发它(EG:请求来自文件X,如果X存在我想要服务该文件,否则处理程序应该处理它)。

这些文件只是静态内容,而不是脚本本身,我认为它使它更容易但我似乎无法找到任何可以做到这一点的任何东西……任何人都有任何想法? 我认为它可以完成,因为IIS7重写模块可以管理它,但我看不出如何…

编辑只是为了澄清…处理程序是典型的情况,它不是error handling例程,而是实际提供适当的内容。 我只是希望能够将新文件作为单独的东西添加到文件夹中,或者作为处理程序将提供的内容的重载。

我最终坚持使用处理程序,而是使用以下方法来解决问题:

 if (File.Exists(context.Request.PhysicalPath)) context.Response.TransmitFile(context.Request.PhysicalPath); else { /* Standard handling */ } 

鉴于有这么多人主张模块和捕捉exception,我觉得我应该澄清为什么我不听:

  1. 这是标准的程序流程,因此我不喜欢使用exception来触发它的想法,除非它变得绝对必要。
  2. 这实际上是在正常情况下返回内容。 HttpModule实际上处理典型请求而不仅仅是做一些基本的共享处理和处理边缘情况的想法似乎有点过时了。 因此,我更喜欢使用HttpHandler,因为它处理典型的请求。

可能你想要实现一个HttpModule。 否则,您正在与争夺请求的所有其他HttpHandler进行斗争。

这应该让你开始……

您可以决定要在请求生命周期中执行检查和响应的位置。 有关背景,请参阅此文章

 using System; using System.IO; using System.Web; namespace RequestFilterModuleTest { public class RequestFilterModule : IHttpModule { #region Implementation of IHttpModule ///  /// Initializes a module and prepares it to handle requests. ///  ///  /// An  that provides access to the methods, /// properties, and events common to all application objects within an ASP.NET application ///  public void Init(HttpApplication context) { context.BeginRequest += ContextBeginRequest; } ///  /// Disposes of the resources (other than memory) used by the module that implements . ///  public void Dispose() { } private static void ContextBeginRequest(object sender, EventArgs e) { var context = (HttpApplication) sender; // this is the file in question string requestPhysicalPath = context.Request.PhysicalPath; if (File.Exists(requestPhysicalPath)) { return; } // file does not exist. do something interesting here..... } #endregion } } 

   ...............................  ...........................      ...................  

如果你不想创建一个HttpModule,我可以想到两个hacks:

  1. 在IIS上使用mod-rewrite或者在IIS上重写II7,以允许存在的特定URL通过,获取其他任何内容并将其重定向到静态文件。 这可能是一个很大的列表,如果你只有少量的文件,我只建议实现这个hack。
  2. 更改您的URL结构以支持路由脚本,该脚本可以检查文件是否存在并在适当时返回。 这种方法可能会影响缓存,所以请谨慎使用。

雅各