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 -
Imports Microsoft.VisualBasic
Public Class JpegImageHandler
Implements IHttpHandler
Public ReadOnly Property IsReusable As Boolean Implements System.Web.IHttpHandler.IsReusable
Get
Return False
End Get
End Property
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")
End If
If req.RawUrl.ToLower().Contains("DryLand1") Then
res.TransmitFile(req.PhysicalApplicationPath & "/Desert.jpg")
End If
If req.RawUrl.ToLower().Contains("WaterAnimal1") Then
res.TransmitFile(req.PhysicalApplicationPath & "/Hydrangeas.jpg")
End If
End Sub
End Class
Then I registered the handler in web.config file by -
(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
Response.Redirect("Flower1.jpg")
End Sub
Protected Sub imbDesert_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles imbDesert.Click
Response.Redirect("DryLand1.jpg")
End Sub
Protected Sub imbHydrangeas_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles imbHydrangeas.Click
Response.Redirect("WaterAnimal1.jpg")
End Sub
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(!).
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!
So weird! I deleted my previous project and restarted a fresh new one with same task. Now everything working! Now it is doing everything whatever I want. This time I tried with IIS 6.0 Version of web.config file. It is working (previously it was not working!
Trust me!). The problem solved, but I don't know whose answer should be chosen as the correct one! Thank you friends for your support.
dutta_sudipt...
Member
67 Points
29 Posts
Http handler not working
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 -
Imports Microsoft.VisualBasic Public Class JpegImageHandler Implements IHttpHandler Public ReadOnly Property IsReusable As Boolean Implements System.Web.IHttpHandler.IsReusable Get Return False End Get End Property 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") End If If req.RawUrl.ToLower().Contains("DryLand1") Then res.TransmitFile(req.PhysicalApplicationPath & "/Desert.jpg") End If If req.RawUrl.ToLower().Contains("WaterAnimal1") Then res.TransmitFile(req.PhysicalApplicationPath & "/Hydrangeas.jpg") End If End Sub End ClassThen 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 Response.Redirect("Flower1.jpg") End Sub Protected Sub imbDesert_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles imbDesert.Click Response.Redirect("DryLand1.jpg") End Sub Protected Sub imbHydrangeas_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles imbHydrangeas.Click Response.Redirect("WaterAnimal1.jpg") End SubLook 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(!).
Please HELP.
hirendaraji
Member
186 Points
39 Posts
Re: Http handler not working
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.
dutta_sudipt...
Member
67 Points
29 Posts
Re: Http handler not working
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!
BrockAllen
All-Star
27574 Points
4912 Posts
MVP
Re: Http handler not working
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.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
dutta_sudipt...
Member
67 Points
29 Posts
Re: Http handler not working
Jun 30, 2012 06:43 PM|LINK