HttpHandler ASHX File Exception Handling

I am using an ASHX file to create images in a dynamic way.
 I added a line of code to throw an exception in my ashx file. if I view the ashx file directly, my application_error in global.asax works fine for error handling.

My problem: application_Error does not work when I use this handler on another page. as in show.aspx if I put:

<img src="image.ashx" />

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

Update: to make things clearer to catch catch. I have 2 options. First of all, redirect the user to another page if for some reason the image generation is not going well. secondly, replace the image with a static png file again, if ANY reason caused some errors in the ashx file.
so 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 this in an OnError?

+3
source share
2 answers

, , . <img> , "" . , .

+2

catch , .

0

Source: https://habr.com/ru/post/1722586/


All Articles