IHttpHandler Issue

Discussion in 'ASP.NET 2.0' started by VirtualRichard, Aug 26, 2008.

  1. I want to replace requests for an image "/Avatars/Avatar.jpg" with a random avatar image. I wrote an HttpHandler to do this.

    It works locally but not when uploaded to discountasp.net. When I try to browse the 'image' I get a 404 Not Found...

    What could be wrong? As the app runs locally, I know all the file locations are ok, etc.

    Richard

    ImageHandler.cs



    using System;


    using System.Configuration;


    using System.IO;


    using System.Web;


    using System.Globalization;


    namespace Avatar


    {


    public class ImageHandler : IHttpHandler


    {


    public void ProcessRequest(System.Web.HttpContext httpContext)


    {


    httpContext.Response.StatusCode = 200;


    httpContext.Response.ContentType = "image/png";


    httpContext.Response.WriteFile(AvatarGenerator.GetAvatar());


    }


    public bool IsReusable


    {


    get


    {


    return true;


    }


    }


    }


    public class AvatarGenerator


    {


    static string avatarDirectory = ConfigurationManager.AppSettings["AvatarDirectory"];


    static int numberOfAvatars = Convert.ToInt32(ConfigurationManager.AppSettings["NumberOfAvatars"]);


    static string avatarImageFormat = ConfigurationManager.AppSettings["AvatarImageFormat"];


    public static string GetAvatar()


    {


    Random random = new Random();


    string randomAvatarId = random.Next(1, numberOfAvatars).ToString();


    string avatarUrl = avatarDirectory + randomAvatarId + avatarImageFormat;


    return avatarUrl;


    }


    }


    }


    web.config





    <configuration>


    <appSettings>


    <add key="AvatarDirectory" value="~/Assets/Images/Avatars/"/>


    <add key="NumberOfAvatars" value="126"/>


    <add key="AvatarImageFormat" value=".png"/>


    </appSettings>


    <system.web>


    <compilation debug="false" />


    <authentication mode="Windows" />


    <customErrors mode="RemoteOnly"/>


    <httpHandlers>


    <add verb="*" path="*.png" type="Avatar.ImageHandler, Avatar"/>


    </httpHandlers>


    </system.web>


    </configuration>
     
  2. Hi,
    A few things I'd try are file permisions and paths.

    In your web.config allow permissions for that image folder like this:

     
  3. I don't think it's that. I've made a few changes and added a trace for the path and everything is fine.

    What's happening is (I believe)the code is never being executed. The call to mydomain/image.png is not being intercepted by IHttpHandler and shows up as a 404 not found as a result...

    Is this something discountasp.net don't allow?? Like I said, it's working just fine on my local machine.

    Richard
     
  4. I'm also having trouble getting my IHttpHandler going on DiscountASP.NET. Just like yours, it works fine locally, but doesn't even fire once its up on the hosted server. I hooked log4net up to it and it's definitely not firing. Did you ever get a resolution on this?
     

Share This Page