Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jun 30, 2012 06:43 PM by dutta_sudipta
Jun 30, 2012 02:11 AM|LINK
I was trying to learn the functionality of HTTP handlers. I developed a simple image request handler in VS 2008 and it is working. But when I tried same thing in VS 2010 I am facing problem.
What I did:
I wrote a Http handler class named JpegImageHandler derived from IHttpHandler interface and implemented IsReusable property and ProcessRequest method. I put it in App_Code folder. Code is -
Public Class JpegImageHandler
Public ReadOnly Property IsReusable As Boolean Implements System.Web.IHttpHandler.IsReusable
Public Sub ProcessRequest(context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest
context.Response.ContentType = "image/jpg"
Dim req As HttpRequest = context.Request
Dim res As HttpResponse = context.Response
If req.RawUrl.ToLower().Contains("Flower1") Then
res.TransmitFile(req.PhysicalApplicationPath & "/Chrysanthemum.jpg")
If req.RawUrl.ToLower().Contains("DryLand1") Then
res.TransmitFile(req.PhysicalApplicationPath & "/Desert.jpg")
If req.RawUrl.ToLower().Contains("WaterAnimal1") Then
res.TransmitFile(req.PhysicalApplicationPath & "/Hydrangeas.jpg")
Then I registered the handler in web.config file by -
<system.webServer> <validation validateIntegratedModeConfiguration="false" /> <modules runAllManagedModulesForAllRequests="true" /> <handlers> <add name="JpegImage" path="*.jpg" verb="*" type="JpegImageHandler" resourceType="Unspecified" preCondition="integratedMode" /> </handlers> </system.webServer>
(I know it works for IIS 7.0 Integrated mode. I also tried the IIS 6.0 version <system.web> & <httpHandler> but failed.)
Then I designed a simple web page with 3 image buttons and handled click events as
Protected Sub imbChrysanthemum_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles imbChrysanthemum.Click
Protected Sub imbDesert_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles imbDesert.Click
Protected Sub imbHydrangeas_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles imbHydrangeas.Click
Look I don't have Flower1.jpg, DryLand1.jpg or WaterAnimal1.jpg, but I have Chrysanthemum.jpg, Desert.jpg and Hydrangeas.jpg. I want to process the requests to those files and redirect to original ones.
Now whenever I am browsing the page and clicking the image buttons it is trying to redirect to Flower1.jpg, Dryland1.jpg or WaterAnimal1.jpg and obviously giving 404 error.
I tried the same thing modifying web.config with IIS 6.0 version and even I checked that the images are there(!).
Jun 30, 2012 03:13 AM|LINK
First thing is that you have to got 404 error means , your http handler called but there is problem with image physical path.
so check your image path return from hander is same as image physical located path.
Jun 30, 2012 07:09 AM|LINK
Thanks Hiren, for raising the helping hand. Actually I don't have Flower1.jpg, Dryland1.jpg or WaterAnimal1.jpg. But I have Chrysanthemum.jpg, Desert.jpg and Hydrangeas.jpg in my Project directory (first I tried to keep them in a different "Image" directory,
which also failed). Now when the http handler class will take the responsibility, the ProcessRequest( ) should be called and the reference to those three files should be redirected to the original files. Now 404 may occur in two situations - if request to
Flower1.jpg is made and it searches for "Flower1.jpg" (bypassing the http handler) or somehow not getting Chrysanthemum.jpg.
The worst(!) thing is the same type of code is working with Visual Studio 2008!
Jun 30, 2012 01:21 PM|LINK
Anytime I can't seem to get IIS to run my module or handler, I enable
FREB and check the log as to where in the pipeline it's failing the call.
Jun 30, 2012 06:43 PM|LINK