How to include HttpHandler for .ashx extension in web.config?

Posted by Prabu_Spark under ASP.NET on 11/29/2012 | Points: 10 | Views : 8138 | Status : [Member] | Replies : 0
Hi sir,
Tell me the code to include HttpHandler for .ashx extension in web.config. Kindly give me
the solution for this problem. I attached the code for the reference.

Web.config:
***********************
<system.web>
<httpHandlers>
<!--<add verb="*" path="ShowImage.ashx" type="ShowImage,System.Web.UI.SimpleHandlerFactory" validate="true" />-->
<!--<add verb="*" path="ShowImage.ashx" type="Web.Framework.HttpHandlers.SomeHandler, Web.Framework, Version=4.0, Culture=neutral" />-->
<add path="*" verb="*" type="System.Web.UI.SimpleHandlerFactory" validate="true" />
</httpHandlers>

</system.web>

Design Page:
****************
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="S.No.">
<ItemTemplate>
<%#((GridViewRow)Container).DataItemIndex+1 %>

</ItemTemplate>

</asp:TemplateField>
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server"
ImageUrl='<%# "ShowImage.ashx?ID=" + Eval("uhid")%>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


C# code for .ashx:
**************************

<%@ WebHandler Language="C#" Class="ShowImage" %>

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


public class ShowImage : IHttpHandler {

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");

if (context.Request.QueryString["ID"] != null)
{
SqlCommand cmd;
// context.Response.Write(context.Request.QueryString["id"]);
string dbcon = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
SqlConnection con = new SqlConnection(dbcon);
con.Open();

cmd = new SqlCommand("SELECT picture FROM photos WHERE uhid =@autoId", con);

cmd.Parameters.AddWithValue("@autoId", context.Request.QueryString["ID"].ToString());
SqlDataReader dr = cmd.ExecuteReader();

while (dr.Read())
{
//context.Response.ContentType = "image/JPEG";
context.Response.BinaryWrite((byte[])dr["picture"]);
}
dr.Close();
con.Close();
}
else
{
context.Response.Write("No Image Found");
}
}

public bool IsReusable {
get {
return true;
}
}

}

With regards,
J.Prabu.
[Email:prbspark@gmail.com]



Responses

(No response found.)

Login to post response