如何处理 ASP.NET 中自定义 HttpHandler 的 null 返回?

发布于 2024-09-04 08:27:16 字数 1422 浏览 4 评论 0原文

我正在使用自定义 ashx HttpHandler 从数据库检索 gif 图像并将其显示在网站上 - 当图像存在时,它效果很好。

然而,在某些情况下,图像不存在,我希望保存图像的 html 表变得不可见,这样就不会显示“图像未找到”图标。

但由于 HttpHandler 不是同步的,我在 Page_Load 检查图像大小的所有尝试都失败了。关于如何实现这一点有什么想法吗?

编辑::

到目前为止,这是如何发生的:

这是我的处理程序:

 public void ProcessRequest(HttpContext context)
        {
            using (Image image = GetImage(context.Request.QueryString["id"]))
            {
                context.Response.ContentType = "image/gif";
                image.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
            }
        }

        private Image GetImage(string id)
        {
            try
            {
                System.IO.MemoryStream ms;
                byte[] rawImage;
                Image finalImage;
                // Database specific code!      
rawImage = getImageFromDataBase(id);

                ms = new System.IO.MemoryStream(rawImage, 0, rawImage.Length);
                ms.Write(rawImage, 0, rawImage.Length); 

                finalImage = System.Drawing.Image.FromStream(ms, true);

                return finalImage;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("ERROR:::: " + ex.Message);
                return null;
            }
        }

我这样使用它:

myImage.ImageUrl = "Image.ashx?id=" + properId;

I'm using a custom ashx HttpHandler to retrieve gif images from a database and show it on a website - when the image exists, it works great.

However, there are cases when the image will not exist, and I'd like to have the html table holding the image to become invisible so the "image not found" icon is not shown.

But since the HttpHandler is not synchronous, all my attempts checking for image size at Page_Load were frustrated. Any ideas on how this can be accomplished?

EDIT::

Here's how it's happening so far:

This is my handler:

 public void ProcessRequest(HttpContext context)
        {
            using (Image image = GetImage(context.Request.QueryString["id"]))
            {
                context.Response.ContentType = "image/gif";
                image.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
            }
        }

        private Image GetImage(string id)
        {
            try
            {
                System.IO.MemoryStream ms;
                byte[] rawImage;
                Image finalImage;
                // Database specific code!      
rawImage = getImageFromDataBase(id);

                ms = new System.IO.MemoryStream(rawImage, 0, rawImage.Length);
                ms.Write(rawImage, 0, rawImage.Length); 

                finalImage = System.Drawing.Image.FromStream(ms, true);

                return finalImage;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("ERROR:::: " + ex.Message);
                return null;
            }
        }

And I use it like this:

myImage.ImageUrl = "Image.ashx?id=" + properId;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

古镇旧梦 2024-09-11 08:27:17

但是,在某些情况下
图像将不存在,我想
让 html 表保存图像
变得不可见,因此“图像不存在”
未显示“找到”图标。

解决此问题的最简单方法是在返回之前检查 Http 处理程序(在 image.ashx 文件中)中是否存在该图像。

  if(image == null) {image = new blankImage();}

如果不存在,请用空白图像替换它这样,如果您确实希望它消失并且不保持图像大小,只需将空白图像设为 1x1 正方形即可。

However, there are cases when the
image will not exist, and I'd like to
have the html table holding the image
to become invisible so the "image not
found" icon is not shown.

The easiest way to fix this is to check to see if the image exists in the Http-handler (in the image.ashx file.) before returning.

  if(image == null) {image = new blankImage();}

If it's not there, substitute it with a blank image. That way this is no image not found icon. If you really want it to disappear and not hold the image size, just make the blank image a 1x1 square.

转角预定愛 2024-09-11 08:27:17

你不能只使用 NullReferenceException,还是我误解了这个问题?

try
{ 
    //try to get the photo
}
catch (NullReferenceException)
{
    //handle the error
}

您还可以检查 image == null 我想,这在您的情况下可能更有意义。

Can you not just use a NullReferenceException, or am I misunderstanding the question?

try
{ 
    //try to get the photo
}
catch (NullReferenceException)
{
    //handle the error
}

You could also check if image == null I suppose, that may make more sense in your situation.

香橙ぽ 2024-09-11 08:27:17

由于 ashx 在 page_load 之后执行,因此您可以使其返回 1x1 正方形,但是如果您想完全隐藏该列,由于生命周期,您将会遇到一些问题。

您可以在页面上创建占位符,并动态构建表格。如果您可以避免使用 ashx,而是在代码隐藏中进行图像检索和渲染,您将能够知道何时隐藏该列

since the ashx gets executed after page_load, you can make it return a 1x1 square, however if you want to entirely hide the column, you are going to have some issues because of the lifecycle.

you could create a placeholder on your page, and build your table dynamically. If you can avoid using an ashx, and instead do the image retrieval and rendering in your code-behind, you will be able to know when to hide the column or not

ら栖息 2024-09-11 08:27:17

尽管这使得页面获取图像两次,但我使用的是小图像并且在很少的页面上,所以我认为这是值得的。

这是我添加到页面的代码:

public static bool CheckImageExistance(string url)
        {
            try
            {
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; 
                request.Method = "HEAD";       

                request.Credentials = CredentialCache.DefaultCredentials;

                HttpWebResponse response = request.GetResponse() as HttpWebResponse; 
                response.Close();
                return (response.StatusCode == HttpStatusCode.OK);
            }
            catch (Exception ex)
            {
                return false;
            }

它按预期工作。感谢您的所有意见。

Even though this makes the page fetch the image twice, I'm using small images and on very few pages, so I think it is worth the cost.

This is the code I added to the page:

public static bool CheckImageExistance(string url)
        {
            try
            {
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; 
                request.Method = "HEAD";       

                request.Credentials = CredentialCache.DefaultCredentials;

                HttpWebResponse response = request.GetResponse() as HttpWebResponse; 
                response.Close();
                return (response.StatusCode == HttpStatusCode.OK);
            }
            catch (Exception ex)
            {
                return false;
            }

And it works as expected. Thanks for all the input.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文