HttpHandler only works if the file does not exist

I am trying to create an HTTP handler to handle all requests to a folder, but I want it to work only if the requested files do not exist (EG: Request comes for file X, if there is X, I would like to service the file, otherwise the handler must deal with it).

The files will only be static content, not the scripts themselves, which I believe makes it a bit easier, but I can't find anything that can do the trick ... Does anyone have any ideas? I assume this can be done since the IIS7 rewriter can manage it, but I donโ€™t see how ...

Edit Just to clarify ... the handler is a typical case, and it is not an error handling procedure, but actually provides relevant content. I just want to add new files to the folder either as separate things, or as overloads for what the handler passed.

+7
source share
3 answers

In the end, I ran into a handler and instead decided to solve the following problem:

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

Given that many people advocate for modules and catch exceptions, I feel that I have to clarify why I did not listen:

  • This is a standard program flow, so I donโ€™t like the idea of โ€‹โ€‹using an exception to run it unless it becomes absolutely necessary.
  • This actually returns the content under normal circumstances. The HttpModule idea, which actually handles typical queries, rather than just doing some basic edge and edge processing, seems a bit off. Therefore, I prefer to use the HttpHandler as it handles typical requests.
+9
source

You might want to implement the HttpModule. Otherwise, you are fighting all the other HttpHandlers who are vying for the request.

That should make you start ...

You can decide where in the life cycle of the request you want to complete your validation and respond. See this article for background

 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> 
+5
source

If you prefer not to create an HttpModule, I can think of two hacks:

  • Use something like mod-rewrite in Apache or II7, rewriting in IIS to allow specific URLs that exist, go through something else, and redirect it to your static file. This can be a large list, and I would recommend implementing this hack if you have only a small number of files.
  • Modify the URL structure to support a script routing that can check if the file exists and return it when necessary. This approach is likely to affect caching, so please be careful.

Jacob

0
source

All Articles