locked
HttpHandler not getting called RRS feed

  • Question

  • User1493956762 posted

    I want to restrict my static images from direct url, like www.abc.com/abc.jpg. So to do that, I added a class, inherit IHttpModule and add some lines in web.config. But that class never called.

     

    here is class code

     

    using System;
    using System.Data;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;

    public class XImageHandler : IHttpHandler
    {
      public void ProcessRequest(HttpContext context)
      {
      if (context.Request.UrlReferrer == null ||
      context.Request.UrlReferrer.Host.Length == 0 ||
      context.Request.UrlReferrer.Host.CompareTo(
      context.Request.Url.Host.ToString()) == 0)
      {
      }
      }

      public bool IsReusable
      {
      get
      {
      return false;
      }
      }
    }

    in web.config
      <httpHandlers>
      <add verb="*" path="*.jpg"
      type="XImageHandler" />
      <add verb="*" path="*.png"
      type="XImageHandler" />
    ......
      </httpHandlers>

     

    any idea ?
     

    Thursday, June 11, 2009 8:05 AM

Answers

  • User1493956762 posted

    Yes I tried that just before HttpHandler but the problem is that its called only when any page loads. Not when an image loads.

     

    PS. I tried HttpModule when I was developing site and it was working, but after that I just commented its 'add' tag in web.config. And when I uncomment it, its not working on image load.

     

    Edit :

    I changed the class name as well as HttpModule name, now ITS WORKING....

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 11, 2009 8:32 AM

All replies

  • User1779468316 posted

    The first thing to do, is use the correct tool for the job. And the correct tool for you is an HttpModule not an HttpHandler. Read on for why...

    An HttpModule has the opportunity to inspect every request that comes in, while
    an HttpHandler is designed to handle a custom file type.

    In your handler, if you only want to handle jpg files, then you'd use something like this:

    HttpApplication app = (HttpApplication) source;
    HttpContext ctx = app.Context;
    string url = ctx.Request.RawUrl;
    if ( url.EndsWith( ".jpg" ) )
    {
      // do your processing here

    }

    Thursday, June 11, 2009 8:26 AM
  • User1493956762 posted

    Yes I tried that just before HttpHandler but the problem is that its called only when any page loads. Not when an image loads.

     

    PS. I tried HttpModule when I was developing site and it was working, but after that I just commented its 'add' tag in web.config. And when I uncomment it, its not working on image load.

     

    Edit :

    I changed the class name as well as HttpModule name, now ITS WORKING....

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 11, 2009 8:32 AM