开发者

How do i create a 404 image? for images that cannot be read by ASP.NET

开发者 https://www.devze.com 2022-12-17 03:42 出处:网络
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

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 unavailabl开发者_C百科e placeholder image?


    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");
        }
    }


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消