I have written the following handler class which is used to read images from database and show them in my web page :
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Web;
public class Handler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["IranQRDBConnectionString"].ConnectionString);
public void ProcessRequest(HttpContext context)
{
try
{
string TableName = context.Session["TableToQuery"].ToString();
string ID = context.Session["ID"].ToString();
SqlCommand comm = new SqlCommand("SELECT * FROM " + TableName + " WHERE ID=" + ID, conn);
conn.Open();
SqlDataReader dr = comm.ExecuteReader();
dr.Read();
context.Response.ContentType = "image/jpeg";
context.Response.BinaryWrite((byte[])dr["Image"]);
conn.Close();
}
catch
{
SqlCommand comm = new SqlCommand("SELECT * FROM DefaultImage WHERE ID=1", conn);
conn.Open();
SqlDataReader dr = comm.ExecuteReader();
dr.Read();
context.Response.ContentType = "image/jpeg";
context.Response.BinaryWrite((byte[])dr["Image"]);
conn.Close();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
This class works fine on my local ! I have uploaded my website and when I query my database all data is returned to my web page except the image is not shown in the image control. I have searched the web and I found out that I should register my handler in the web.config file and also IIS version is 7 on the host and It’s running in integrated mode ! So I know that I should register that handler in <System.webserver><Handlers> part of the web.config .
For more detail I added the handler class to my project root directory and not in the App_Code directory ! And I have uploaded the website as precompiled website and I have the precompiled Handler.ashx in my root directory and I have App_Web_handler.ashx.cdcab7d2.dll file in my App_Code .
I also have added :
<add name="ImageHandler" verb="*" path="*.jpg" type="Handler" />
in my web.config file but it still does not work 🙁
Could any one help please me find the correct registration ?
And here comes the answer ! there was nothing wrong with my code , and there was a firewall on the host wich did not allow my images to be showen ! after configuring the firewall my project works just fine 🙂