Reputation: 2622
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
}
Upvotes: 6
Views: 6946
Reputation: 4768
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.
Upvotes: 1
Reputation: 1500
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
Upvotes: 0
Reputation: 21887
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);
}
}
Upvotes: 9
Reputation: 5746
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.
Upvotes: 4