6

I am looking at creating a handler to return images based on id passed through, I haven't created my own one before and when I created it it mentions that it has to be registered with IIS. This project is distributed to a lot of clients, will I have to change each one's IIS or is there some way round this or an alternative to a handler?

EDIT: In response to below, this is what I have created (but not yet tested), so will I need to change anything in IIS or web.config for this?

public class Photos : IHttpHandler
{
    #region IHttpHandler Members

    public bool IsReusable
    {
        get { return true; }
    }

    public void ProcessRequest(HttpContext context)
    {
        //write your handler implementation here.
        var img = Image.FromFile(@"C:\Projects\etc\logo.jpg");
        context.Response.ContentType = "image/jpeg";
        img.Save(context.Response.OutputStream, ImageFormat.Jpeg);
    }

    #endregion
}
2
  • What have you got so far? Commented Jan 30, 2014 at 15:42
  • See edit, builds ok but not returning image despite checking path is correct. Commented Jan 30, 2014 at 15:56

4 Answers 4

9

You can create a class that inherits IHttpHandler and within that grab the id (from the querystring or similar), process the request and return the binary data. Shouldn't have to register it with IIS...

public class MyHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        //Get Id from somewhere

        //Get binary data

        context.Response.ContentType = "application/octet-stream";
        context.Response.BinaryWrite(bytes);
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Yes, that looks fine to me!
Seconded, that is the way to solve this thing, and the jpeg return type is better than octet-stream, makes it more obvious to clients what they're getting.
Thanks, tested using <img src='Photos.ashx' /> but image is not displayed. Is it possible I've missed something or does it have to be an asp image control?
@WillemDuncan yes! I believe his question prior to edit mentioned returning binary data which is why I set octect-stream...
@user1166905 what happens when you navigate to http://localhost:{port}/Photos.ashx?
|
4

You can add a generic handler to your project (a .ashx file). It will give you a file with a codebehind as follows (excerpt):

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

You can use the context to get query string or route parameters, and the context.Response property to write your image. Change the returned content type to your image content type, and you're set.

You would set your image src to: "Handler1.ashx?id=12345", or you could add a pretty url route pointing to the handler.

No need to configure IIS for this.

Comments

1

You can look at this blogpost about Handlers, http://www.dotnetperls.com/ashx, which I think is pretty close to what you want to do.

Comments

0

The httphandlers are registered in web.config of website, if you distribute the config file with website you don't need to change iis configuration

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.