只有当文件不存在的HttpHandler火 [英] HttpHandler fire only if file doesn't exist

查看:117
本文介绍了只有当文件不存在的HttpHandler火的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创造一个HTTP处理程序来处理到一个文件夹中所有的要求,但我只希望它如果请求不存在的文件(如火灾:请求进入文件X,如果X存在我会希望提供服务的文件,否则处理程序应处理它)。

I'm trying to create a HTTP handler to handle all requests to a folder but I only want it to fire if the files requested don't exist (EG: Request comes in for file X, if X exists I'd like to serve the file, otherwise the handler should deal with it).

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

The files will only be static content, not scripts themselves, which I assume makes it a bit easier but I can't seem to find anything that will do the trick... Anyone have any ideas? I assume it can be done since the IIS7 rewrite module can manage it, but I can't see how...

修改只是为了澄清...的处理程序是典型的例子,它是不是一个错误处理例程,但实际上提供适当的内容。我只是想将新文件添加到该文件夹​​作为单独的东西或过载哪些处理程序将提供的能力。

Edit Just to clarify... the handler is the typical case and it is not an error-handling routine but actually delivering appropriate content. I just want the ability to add new files to the folder either as separate things or as overloads to what the handler would deliver.

推荐答案

我已经结束了与处理器贴合,而不是使用下面要解决的问题:

I've ended up sticking with a handler and instead using the following to solve the problem:

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

由于这么多的人主张模块和捕获异常,我觉得我要澄清我为什么不听:

Given so many people advocated Modules and catching exceptions I feel I should clarify why I didn't listen:


  1. 这是标准的程序流程,因此我不喜欢使用一个例外,除非绝对需要,才可触发它的想法。

  2. 这实际上是回归正常情况下的内容。一个HTTP模块实际处理典型请求,而不是仅仅做一些基本的共享的加工和处理边缘案件的想法似乎有点过。因此我preFER使用一个HttpHandler的,因为它是处理典型的请求。

这篇关于只有当文件不存在的HttpHandler火的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆