代码之家  ›  专栏  ›  技术社区  ›  fyjham

仅当文件不存在时才激发httphandler

  •  7
  • fyjham  · 技术社区  · 16 年前

    我正试图创建一个HTTP处理程序来处理对文件夹的所有请求,但我只希望在请求的文件不存在时触发它(例如:请求进入文件x,如果x存在,我想服务于该文件,否则处理程序应该处理它)。

    这些文件只会是静态内容,而不是脚本本身,我认为这会使它变得简单一点,但我似乎找不到任何可以实现此功能的东西…有人有什么想法吗?我认为这是可以做到的,因为iis7重写模块可以管理它,但我不知道如何…

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

    3 回复  |  直到 16 年前
        1
  •  9
  •   fyjham    16 年前

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

    1. 这实际上是在正常情况下返回内容。实际上,HTTPmodule处理典型请求而不仅仅是进行一些基本的共享处理和处理边缘案例的想法似乎有点离谱。因此,我更喜欢使用httphandler,因为它处理的是典型的请求。
        2
  •  5
  •   Sky Sanders    16 年前

    可能您希望实现一个httpmodule。否则,您将与所有其他正在争夺请求的httphandler进行斗争。

    这应该能让你开始……

    您可以决定在请求生命周期中要执行检查和响应的位置。见 this article 为了一些背景

    using System;
    using System.IO;
    using System.Web;
    
    namespace RequestFilterModuleTest
    {
        public class RequestFilterModule : IHttpModule
        {
            #region Implementation of IHttpModule
    
            /// <summary>
            /// Initializes a module and prepares it to handle requests.
            /// </summary>
            /// <param name="context">
            /// An <see cref="T:System.Web.HttpApplication"/> that provides access to the methods, 
            /// properties, and events common to all application objects within an ASP.NET application 
            /// </param>
            public void Init(HttpApplication context)
            {
                context.BeginRequest += ContextBeginRequest;
            }
    
            /// <summary>
            /// Disposes of the resources (other than memory) used by the module that implements <see cref="T:System.Web.IHttpModule"/>.
            /// </summary>
            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
        }
    }
    

    <?xml version="1.0"?>
    <configuration>
        ...............................
        <system.web>
        ...........................
            <httpModules>
                <add name="RequestFilterModule" type="RequestFilterModuleTest.RequestFilterModule"/>
                <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            </httpModules>
        </system.web>
        ...................
    </configuration>
    
        3
  •  0
  •   TheJacobTaylor    16 年前

    如果您不想创建一个httpmodule,我可以考虑两个黑客:

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

    雅各伯