如何处理 ASP.NET 中自定义 HttpHandler 的 null 返回?
我正在使用自定义 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
解决此问题的最简单方法是在返回之前检查 Http 处理程序(在 image.ashx 文件中)中是否存在该图像。
如果不存在,请用空白图像替换它这样,如果您确实希望它消失并且不保持图像大小,只需将空白图像设为 1x1 正方形即可。
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 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.
你不能只使用 NullReferenceException,还是我误解了这个问题?
您还可以检查
image == null
我想,这在您的情况下可能更有意义。Can you not just use a NullReferenceException, or am I misunderstanding the question?
You could also check if
image == null
I suppose, that may make more sense in your situation.由于 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
尽管这使得页面获取图像两次,但我使用的是小图像并且在很少的页面上,所以我认为这是值得的。
这是我添加到页面的代码:
它按预期工作。感谢您的所有意见。
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:
And it works as expected. Thanks for all the input.