locked
Problem with folders using a catch-all httphandler RRS feed

  • Question

  • User-1591584071 posted

    First I'll explain what I want to achieve. I want all httprequests for url's in my application that do not really exist to be handled by a function of mine. For example: http://www.example.com/a/b/c and http://www.example.com/test.abc do not really exist, so they should be handled by my function. http://www.example.com/picture.jpg does exist, so that one should not be handled by my function.

    I've accomplished a lot using a httphandler. In IIS I've configured the application to send all extensions through the .NET engine. (In the tab Home Directory, click on Configuration, then add extension .* for executable aspnet_isapi.dll, limit to GET,HEAD,POST,DEBUG and turn of Check that file exists.

    In web.config I added the following lines:

    <httpHandlers>
       <add verb="GET,HEAD,POST,DEBUG" path="*" type="MyHttpHandler, Assembly" />
    </httpHandlers>

    And in Assembly.cs I added this:

    using System;
    using System.Web;
    public class GlobalHttpHandler : IHttpHandler
    {
     public void ProcessRequest(HttpContext context)
     {
      HttpRequest Request = context.Request;
      HttpResponse Response = context.Response;

      Response.Write("The HttpHandler works!");
     }

     public bool IsReusable
     {
      get { return false; }
     }

    I've got two problems. The first one is that when I visit the existing folder http://www.example.com/css (without a final slash!) I get prompted to log in, which only works with the administrator password, and when I do I see a page with the following code:

    <html>-2146893039 (0x80090311)</body></html>

    This only occurs when an existing folder is addressed without a final slash. http://www.example.com/css/ leads to my function, and so does the nonexisting http://www.example.com/abc. This bug seems to be caused by the .* reference in IIS. Replacing .* with just ., makes the httphandlers work fine for all folders directed without a final slash. But then that's all the httphandler catches. Using both .* and . gives the same result as just using .*

    My second problem is that existing files are ignored. http://www.example.com/picture.jpg for example shows my function and not the picture, eventhough is does exist.

    A possible solution is altering the 404 error to lead to a url (/404handler.aspx for example). In that case all nonexisting folder and file requests are redirected to this .aspx file. However, the server does keep track of these 404's, which annoys system administrators. And I thought using httphandlers was the "official" .net-way to go for this.

    Help? Anyone?

    Thursday, December 15, 2005 4:22 AM

All replies

  • User-1128085056 posted

    Assuming that you are using ASP.NET 2.0 you can solve your problem in a fairly straight forward way. Instead of an HttpHandler, create an HttpHandlerFactory and call ASP.NET's page builder logic, trapping the not found exception:

     

     

    using System;
    using System.Web;
    using System.Web.Compilation;

    namespace Handlers
    {

    public class MyHttpHandlerFactory : System.Web.IHttpHandlerFactory
    {
       public IHttpHandler GetHandler( HttpContext context, string requestType, string url, string pathTranslated)
      {
              try
             {
                   return (IHttpHandler)BuildManager.CreateInstanceFromVirtualPath(url, typeof(Page));
             }
             catch( HttpException ex )
             {
                   // Handle your missing page here.
     
                  return MySpecialHandler;
             }
    }

    }

    To get your .jpg file to be served, you will have to add an http handler for it in your web.config before the entry for the handler factory:

    <httpHandlers>
       <add type="System.Web.StaticFileHandler" path="*.jpg" verb="*"/>
      <!-- repeat for any other file types that your want served -->
       <add type="Handlers.MyHttpHandlerFactory, Assembly" path="*.png" verb="*"/>
    </httpHandlers>


     

     

     

       

    Thursday, December 15, 2005 11:25 PM