user1778924
user1778924

Reputation: 19

Generic Handler not getting called in asp.net

I am trying to display an image from my database. I have an generic handler to display the image. But my problem is that it doesn't get called. My code for calling the handler is

Image1.ImageUrl = "~/ShowImage.ashx?id=" + id;

where id is a number and ShowImage.ashx is the handler. The breakpoints in .ashx file doesn't get hit either. I am new to asp.net. So any help would be highly appreciated.

Upvotes: 2

Views: 3124

Answers (2)

Jayesh Sorathia
Jayesh Sorathia

Reputation: 1614

Check this sample Example code this might help you.

ASPX Code :

<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# Examples (ImageHandler.ashx File) :

<%@ 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;
        }
    }

}

Here is live downloadable C# Examples and VB.Net Examples of this. Click Here...

Upvotes: 1

Aristos
Aristos

Reputation: 66641

In this cases the steps that you need to follow is to see how the html is rendered.

So, right click on the html page, and "view page source".

There locate the point that the ShowImage.ashx is called, and see if the full rendered path is correct.

From there you simple correct the path.

Additional you can use the browser tools to see what browser looks for, and if he finds it or not. On google chrome for example you make right click, then inspect elements and then click on the network. There you can see with red, what files your page can not find, and you need to fix the path.

Upvotes: 1

Related Questions