views:

184

answers:

3

Using ASP.NET C#, visual studios to host the files/pages. I allow users to upload images and i redirect them to the media page. The images are in a specific path easily recognizable with regex. When i redirect i get 1 of 2 errors. 1) Image doesnt exist (hasnt been generated) or 2) Cannot open file (imagemagik or something writing to the image).

When i get theses errors how do i stop the 404 or other error and display 'processing' or currently unavailable placeholder image?

A: 

Well 404 is the correct response code, but you might be able to set a custom handler on that in the images folder to instead return a dummy image.

EDIT: To set up the custom handler in IIS manager, go to the site, and go to Error Pages.

Yuliy
yes so HOW do i do that?
acidzombie24
I've edited the response
Yuliy
I am using VisualStudios and not IIS but either way, would that set up the handler for all 404 images? Is there a way i can set specific images (using regex to match) to a 404 if they dont exist or cannot be read?
acidzombie24
A: 

From the theoretical perspective, couldn't you implement a custom HTTP handler based on your path, similar to what's detailed here: http://support.microsoft.com/kb/308001

Implement the logic to determine if the file requested exists and can be read in the handler. If not, programmatically generate the output you want instead of a 404.

Bryan Redd
here would i do the check in? i know photobucket generates 404 images but they use php. It isnt often when photos are processsed so i prefer not to check everytime, just when an exception occurs.
acidzombie24
If it works as I understand, you'd specify the handler in a web.config file in the path you're concerned with, limiting how often you're calling the handler. Yuliy's suggestion would be more direct and less involved if it works and accomplishes what you need, provided you can specify at the directory level.
Bryan Redd
+2  A: 
    protected void Application_Error(Object sender, EventArgs e)
    {
        var ex = Server.GetLastError();
        var exx = ex as HttpException;
        HttpContext ctx = HttpContext.Current;
        var url = MyCode.CleanPath(ctx);
        if (MyCode.SomeRegexPattern.IsMatch(url))
        {
            var code = ctx.Response.StatusCode;
            if (exx != null)
                ctx.Response.StatusCode = exx.GetHttpCode();
            else if (ex.Message.IndexOf("cannot access the file") != -1)
                ctx.Response.StatusCode = 500;
            ctx.Response.ContentType = "image/gif";
            Server.Transfer("path/to/processing.gif");
        }
    }
acidzombie24
You'll also need to be trapping 404 requests for non- .NET pages - be default these are usually handled by IIS, so an application error isn't raised. Copy this into a shared class, tell IIS to use a custom .aspx page for 404 errors, and handle it in there.
Zhaph - Ben Duguid