asp.net ashx 请求 404
我正在使用 ashx 请求处理程序来检索图像,并且 ashx 文件中的断点没有被命中。当我使用 firebug 时,我可以看到请求返回 404,这让我认为我需要配置一些设置以便可以找到 ashx 文件。
我正在使用 Visual Studio 2008 和 .net 3.5。
ASHX 文件
namespace hybrid.content.Handlers
{
public class DB_Images : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
Int32 image_id;
if (context.Request.QueryString["id"] != null)
image_id = Convert.ToInt32(context.Request.QueryString["id"]);
else
throw new ArgumentException("No parameter specified");
context.Response.ContentType = "image/jpeg";
Stream strm = GetImageFromDatabase(image_id);
if (strm != null)
{
byte[] buffer = new byte[4096];
int byteSeq = strm.Read(buffer, 0, 4096);
while (byteSeq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 4096);
}
//context.Response.BinaryWrite(buffer);
}
}
public Stream GetImageFromDatabase(int image_id)
{
SqlConnectionStringBuilder connstr = new SqlConnectionStringBuilder();
//connstr.InitialCatalog = "dummy";
//connstr.UserID = "sa";
//connstr.Password = "password";
//connstr.DataSource = "source";
connstr.InitialCatalog = "smsdb";
connstr.UserID = "user";
connstr.Password = "password";
connstr.DataSource = "10.31.4.79";
SqlConnection conn = new SqlConnection(connstr.ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
// cmd.CommandText = "select image from cis_images where image_id = @p_image_id";
cmd.CommandText = "select image from test_images where image_id = @p_image_id";
cmd.Parameters.AddWithValue("@p_image_id", image_id);
conn.Open();
object img = cmd.ExecuteScalar();
try
{
return new MemoryStream((byte[])img);
}
catch
{
return null;
}
finally
{
conn.Close();
conn.Dispose();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
页面点击事件
protected void Button1_Click(object sender, EventArgs e)
{
Image1.ImageUrl = "~/DB_Images.ashx?id=" + TextBox1.Text;
}
ashx html
<%@ WebHandler Language="C#" CodeBehind="DB_Images.ashx.cs" Class="hybrid.content.Handlers.DB_Images" %>
除了该引用之外,没有任何 html。
为了让它发挥作用,我是否缺少一些东西?
I am using an ashx request handler to retrieve images and my breakpoint in the ashx file isn't being hit. When I use firebug I can see that the request is returning a 404 which makes me think that I need to configure some setting so that ashx file can be found.
I am using visual studio 2008 and .net 3.5.
ASHX file
namespace hybrid.content.Handlers
{
public class DB_Images : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
Int32 image_id;
if (context.Request.QueryString["id"] != null)
image_id = Convert.ToInt32(context.Request.QueryString["id"]);
else
throw new ArgumentException("No parameter specified");
context.Response.ContentType = "image/jpeg";
Stream strm = GetImageFromDatabase(image_id);
if (strm != null)
{
byte[] buffer = new byte[4096];
int byteSeq = strm.Read(buffer, 0, 4096);
while (byteSeq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 4096);
}
//context.Response.BinaryWrite(buffer);
}
}
public Stream GetImageFromDatabase(int image_id)
{
SqlConnectionStringBuilder connstr = new SqlConnectionStringBuilder();
//connstr.InitialCatalog = "dummy";
//connstr.UserID = "sa";
//connstr.Password = "password";
//connstr.DataSource = "source";
connstr.InitialCatalog = "smsdb";
connstr.UserID = "user";
connstr.Password = "password";
connstr.DataSource = "10.31.4.79";
SqlConnection conn = new SqlConnection(connstr.ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
// cmd.CommandText = "select image from cis_images where image_id = @p_image_id";
cmd.CommandText = "select image from test_images where image_id = @p_image_id";
cmd.Parameters.AddWithValue("@p_image_id", image_id);
conn.Open();
object img = cmd.ExecuteScalar();
try
{
return new MemoryStream((byte[])img);
}
catch
{
return null;
}
finally
{
conn.Close();
conn.Dispose();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
Page click event
protected void Button1_Click(object sender, EventArgs e)
{
Image1.ImageUrl = "~/DB_Images.ashx?id=" + TextBox1.Text;
}
ashx html
<%@ WebHandler Language="C#" CodeBehind="DB_Images.ashx.cs" Class="hybrid.content.Handlers.DB_Images" %>
There isn't any html apart from that reference.
Is there something that I am missing to get this to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
就我而言,我将 ashx 文件标记为“无”内容类型而不是“内容”,即“属性 ->”构建操作应该是内容,这意味着发布网站时不包含 ashx 文件。
In my case I had the ashx file marked as a None content type instead of as Content, i.e. the Properties -> Build Action for it should have been Content, which mean that the ashx file wasn't included when Publishing the site.
如果您在 ASP.NET 中使用通用处理程序,则需要检查一些事项。
1.) 如果确实是 32 位,请确保已将应用程序池中的“32 位应用程序”标志设置为 32 位。默认值为“假”。
2.) 将应用程序池从集成转换为经典
3.) 适当更改应用程序池中的 .NET 版本。在您的情况下,请使用 v2,因为 3.5 使用版本 2。.NET 4.0 使用 .NET 4.0。
4.) 确保 ASP.NET 已注册。运行代码块中的所有内容。
C:\>
cd C:\Windows\Microsoft.NET\Framework64\{版本}
C:\Windows\Microsoft.NET\Framework64{version}>
aspnet_regiis.exe -i
5.) 右键单击服务器名称(不是站点名称)后选择“ISAPI & CGI Restrictions” )在 IIS 管理器中,右键单击正确的“ASP.NET {版本}”行并选择“允许”。
6.) 确保您的处理程序映射(对于 *.ashx)在服务器级别、站点级别或在 web.config 中显式打开(也称为“启用”)。
If you're using Generic Handlers in ASP.NET, there are a few things to check on.
1.) Make sure you have set the "32 bit application" flag in your application pool to 32 bit if it really is 32 bit. The default is "False".
2.) Turn the application pool from Integrated to Classic
3.) Change the .NET version in your application pool appropriately. In your case, use v2, since 3.5 uses version 2. .NET 4.0 uses .NET 4.0.
4.) Make sure ASP.NET is registered. Run everything in the code blocks.
C:\>
cd C:\Windows\Microsoft.NET\Framework64\{version}
C:\Windows\Microsoft.NET\Framework64{version}>
aspnet_regiis.exe -i
5.) Choose the "ISAPI & CGI Restrictions" after right clicking the server name (not the site name) in IIS Manager, and right click the correct "ASP.NET {version}" line and chose "Allow".
6.) Make sure you have your Handler Mappings (for *.ashx) turned on (aka "Enabled") at the server level, or site level or explicitly in the web.config.
我认为问题出在网址上。
~/
表示您的根目录。要获取有关此错误的详细信息,请检查图像的来源并将其复制并粘贴到地址栏中。将弹出包含详细信息的黄页。
我认为唯一的问题是网址。
I think the problem is in the url.
~/
means your root directory .To get details about this error check the image's source and copy and paste it in the address bar. A yellow page consisting of the details will popup.
I think the only problem is the url.