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

fyj*_*ham 7 c# asp.net iis-7 httphandler

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

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

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

fyj*_*ham 9

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

if (File.Exists(context.Request.PhysicalPath)) context.Response.TransmitFile(context.Request.PhysicalPath);
else { /* Standard handling */ }
Run Code Online (Sandbox Code Playgroud)

鉴于有这么多人提倡模块和捕捉异常,我觉得我应该澄清为什么我不听:

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


Sky*_*ers 5

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

这应该让你开始......

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

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
    }
}
Run Code Online (Sandbox Code Playgroud)
<?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>
Run Code Online (Sandbox Code Playgroud)