asp.net - 如何创建 404 图像?对于 ASP.NET 无法读取的图像

标签 asp.net image error-handling http-status-code-404

使用 ASP.NET C#,Visual Studio 来托管文件/页面。我允许用户上传图片并将它们重定向到媒体页面。图像位于特定路径中,可以使用正则表达式轻松识别。当我重定向时,我得到 2 个错误中的 1 个。 1)图像不存在(尚未生成)或 2)无法打开文件(imagemagik 或写入图像的东西)。

当我收到这些错误时,如何停止 404 或其他错误并显示“正在处理”或当前不可用的占位符图像?

最佳答案

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

关于asp.net - 如何创建 404 图像?对于 ASP.NET 无法读取的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2125692/

相关文章:

c# - 在表单标记中设置操作会生成错误 "Validation of viewstate MAC failed..."

.net - 在午夜或没有分钟和秒时获取今天的日期

asp.net - 在 ASP.NET 中,什么时候应该使用 Session.Clear() 而不是 Session.Abandon()?

reactjs - 如何在 src 文件夹之外要求具有反应 Electron 的图像(应用程序支持)

error-handling - 4xx客户端错误的问题

c# - ASP.NET MVC SqlException不会重定向到错误页面

c# - 为什么在 Url 中使用非枚举值

css - add_image_size wordpress 裁剪数组(顶部、底部)不起作用

html - 如何在 CSS 中在另一个背景图像下显示背景图像

javascript - TypeScript 中的异常嵌套/包装