Search code examples
c#asp.netihttphandler

HTTP Handler doesn't fire


I have a HTTP Handler which does not fire. i just noticed that adding a break to my code.

The strange thing is the page where the handler is called and the HTTPhandler have the same namespaces ... I really don't understand why ....

Did anyone face that lately ? thanks in advance

EDIT:

My ImageHandler.ashx.cs:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace Actilog.Parametre.Famille
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class ImageHandler : IHttpHandler
    {

        string strcon = ConfigurationManager.AppSettings["DB_CRACQConnectionString"].ToString();
        public void ProcessRequest(HttpContext context)
        {
            string imageid = context.Request.QueryString["ImID"];
            SqlConnection connection = new SqlConnection(strcon);
            connection.Open();
            SqlCommand command = new SqlCommand("select Image from Table_test where ImageID=" + imageid, connection);
            SqlDataReader dr = command.ExecuteReader();
            dr.Read();
            context.Response.BinaryWrite((Byte[])dr[0]);
            connection.Close();
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

the page where i call it:

<asp:GridView ID="gvImages" runat="server" AutoGenerateColumns="False" 
    CssClass="Gridview" HeaderStyle-BackColor="#7779AF" 
    HeaderStyle-ForeColor="white">
    <Columns>
        <asp:BoundField DataField="ImageName" HeaderText="Image Name" />
        <asp:TemplateField HeaderText="Image">
            <ItemTemplate>
                <asp:Image ID="Image1" runat="server" Height="150px" 
                    ImageUrl='<%# "ImageHandler.ashx?ImID="+ Eval("ImageID") %>' Width="150px" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

my Web.config part:

<httpHandlers>
  <add path="Telerik.Web.UI.WebResource.axd" type="Telerik.Web.UI.WebResource" verb="*" validate="false" />
  <remove verb="*" path="*.asmx" />
  <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
      validate="false" />
  <!-- *******  Register the RadUploadProgressHandler for IIS prior to v.7  ****** -->
  <add path="Telerik.RadUploadProgressHandler.ashx" type="Telerik.Web.UI.RadUploadProgressHandler"
      verb="*" validate="false" />
</httpHandlers>

Solution

  • In your web.config you should add:

    <httpHandlers>
        ...
        <add path="ImageHandler.ashx" type="Actilog.Parametre.Famille.ImageHandler"
          verb="*" validate="false" />
        ...
    </httpHandlers>
    

    To allow Asp.Net to manage every requests containig "ImageHandler.ashx" with the correct IHttpHandler class. And remember to add:

    <system.webServer>
        <handlers>
        ...
        <add name="ImageHandler" path="ImageHandler.ashx" type="Actilog.Parametre.Famille.ImageHandler"
              verb="*" validate="false" />
        ...
        </handlers>
    </system.webServer>
    

    In your production environment (if your using IIS7+).