【问题标题】:Generic Handler not getting called in asp.net通用处理程序没有在 asp.net 中被调用
【发布时间】:2012-10-17 09:14:08
【问题描述】:
我正在尝试显示我的数据库中的图像。我有一个 通用处理程序 来显示图像。但我的问题是它没有被调用。我调用处理程序的代码是
Image1.ImageUrl = "~/ShowImage.ashx?id=" + id;
其中 id 是一个数字,ShowImage.ashx 是处理程序。 .ashx 文件中的断点也不会被命中。我是 asp.net 的新手。因此,我们将不胜感激任何帮助。
【问题讨论】:
标签:
asp.net
image
httphandler
【解决方案1】:
在这种情况下,您需要遵循的步骤是查看 html 是如何呈现的。
所以,右键单击html页面,然后“查看页面源代码”。
这里定位到ShowImage.ashx被调用的点,看完整的渲染路径是否正确。
从那里你可以简单地纠正路径。
另外,您可以使用浏览器工具查看浏览器在寻找什么,以及他是否找到。例如,在谷歌浏览器上,您右键单击,然后检查元素,然后单击网络。在那里你可以看到红色,你的页面找不到哪些文件,你需要修复路径。
【解决方案2】:
查看此示例示例代码,这可能会对您有所帮助。
ASPX 代码:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>
HTTP Handler class Impliment in Img tag
</h1>
<h1>Id : 1</h1>
<img src="ImageHandler.ashx?id=1" alt="Dynamic Image" />
<h1>Id : 2</h1>
<img src="ImageHandler.ashx?id=2" alt="Dynamic Image" />
</div>
</form>
</body>
</html>
C# 示例(ImageHandler.ashx 文件):
<%@ WebHandler Language="C#" Class="ImageHandler" %>
using System;
using System.Web;
public class ImageHandler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
//context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
context.Response.ContentType = "image/jpeg";
if (context.Request.QueryString["id"] == "1")
{
context.Response.WriteFile("bamboo.jpg");
}
else
{
context.Response.WriteFile("palmtree.jpg");
}
}
public bool IsReusable {
get {
return false;
}
}
}
这是可现场下载的 C# 示例和 VB.Net 示例。 Click Here...