I have a class which implements IHttpHandler that is designed to handle image resize requests. It handles Urls like so
http://mysite.com/imageHandler?image=myimg.jpg&width=100&height=100
Currently the handler looks for myimg.jpg on disk, cuts a 100×100 thumbnail (if it isn’t already present) and redirects the client to the thumbnail like so
Response.RedirectPermanent("/some/virtualPath/to/thumbnail.jpg");
This has been working great, but I would like to avoid forcing the client to issue a second HTTP request. Is it safe to do the following?
Server.Transfer("/some/virtualPath/to/thumbnail.jpg")
All the MSDN documentation talks about using Server.Transfer() to redirect to an aspx page, so I’m not sure if this is the right thing to do or not.
Thanks,
Well, the MSDN page explicitly says:
So, even if it might work, it’s not “safe” in the sense that you can rely on this feature. You violate the contract by using a non-aspx page, so, theoretically, the method is allowed to behave arbitrarily.
A safe solution for your problem would be to send the thumbnail to the client using appropriate methods of the
Responseobject, such asBinaryWrite(if the thumbnail image is in memory) orTransmitFile(if the image is on the disk). In that case, don’t forget to set the HTTP header appropriately (Response.ContentType = "image/jpeg") to inform the client that this is a jpg image. The additional advantage of this method is that your thumbnail files no not need to reside in a publicly accessible directory of your web server.