views:

432

answers:

2

I'm using ASHX file to creating images in a dynamic way.
I added a line of code to throw an exception in my ashx file. if I browse to ashx file directly, my application_error in global.asax is working fine to handle the error.

my problem is application_Error is not working when I use this handler in another page. like in show.aspx if I put:

<img src="image.ashx" />

there would be no errors, just a blank image. I even tried a custom httpmodule to handle errors, but no luck. so how can I catch errors in ashx file?

Update: to make things more clear, for exception catching I have 2 options in mind.
first is to redirect user to another page if for ANY reason, image generation goes wrong.
second is to replace the image with my static png file again if ANY reason caused some errors in ashx file.
that's why I thought using httpapplication.error would help. my httpmodule is like:

Public Sub Init(ByVal context As System.Web.HttpApplication) Implements system.Web.IHttpModule.Init
    AddHandler context.Error, AddressOf OnError
End Sub

Private Sub OnError(ByVal sender As Object, ByVal e As EventArgs)
    Dim app As HttpApplication = CType(sender, HttpApplication)
End Sub

So how can I achieve these in OnError ?

+1  A: 

I believe the exception is being thrown as expected. However, now the exception details are serving as the data for your < img>, which will display a 'broken' image. I would suggest serving up a different, static image if there's an error in the handler and log the error in an external text file or some other data source.

ianpoley
what if I want to use redirect ?
mohamadreza
Hmm, ideally, how would you like it to work? If the generated image throws an error, redirect the user?
ianpoley
please check edited question
mohamadreza
A: 

You can add a catch block as normal, and in the event of an error, you could display another default image.

IrishChieftain
I used several external APIs and a bunch of classes to generate this image. I dont like to have tens of try-catch blocks
mohamadreza