question regarding this httpmodule

G

Guest

I wrote a very simple httpmodule and tried to compile it with no success.
This is my code:

==============
Imports System
Imports System.Web
Imports Microsoft.VisualBasic

NameSpace myErrorHandler

Public Class myExceptionHandler
Implements IHttpModule

Public Sub Init (objApp as HttpApplication)
AddHandler objApp.Error, AddressOf Me.OnError
End sub

Public Sub Dispose()

End Sub

Public Sub OnError(Sender as Object, e as EventArgs)
Dim objApp as HttpApplication
objApp = CType(Sender, HttpApplication)
Dim ctx as HttpContext = objApp.Context.Current
Dim exception as Exception = ctx.Server.GetLastError()

Dim errorInfo as string = _
"<br>Offending URL: " & ctx.Request.Url.ToString() & _
"<br>Source: " & exception.Source & _
"<br>Message: " & exception.Message & _
"<br>Stack trace: " & exception.StackTrace

ctx.Response.Write (errorInfo)
End Sub

End Class

End Namespace
==============

I am not too sure how to compile the above code because whenever I tried it,
the vbc compiler said that I have to have init and dispose in order to
implement the IHttpModule interface. The problem is, I do have those 2 subs
in my class?

Can someone point out what's wrong with my code for me?

I used the following command in C:\WINNTS\Microsoft.NET\Framework\v1.1.4322
to compile the code:

vbc /t:library /r:System.dll,System.web.dll PGDErrorHandler.vb
 
K

Kevin Yu [MSFT]

Hi,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to implement the IHttpModule
in your own class. If there is any misunderstanding, please feel free to
let me know.

In VB.NET, we have add Implements keyword to indicate that the method
implements certain method in an interface. Or the method is considered as a
different one. Here I make some changes to your code. HTH.

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

Public Sub Dispose() Implements System.Web.IHttpModule.Dispose

End Sub

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

Thanks Kevin, the module now compiles correctly. however, I am still not too
sure how to use it to response.write out an error message with the module. I
included a reference in web.config, and created a page with a unhandled
runtime error in it. No matter what error I have, it just does not display
the error message based on what I specified in the module. All it shows is
the standard ASP.NET error page.
 
S

Steven Cheng[MSFT]

Hi ,

I think your code in the OnError handler did get executed. The problem that
you still get the buildin red/yellow exception page is because you didn't
end the response in your OnError handler so that the asp.net runtime
continue to pass the request to the default unhandled error handler and
produce the default exception page.
To avoid this and only output our own error info, we can manualy end the
response in Error event after we've output our own content. For example:

Public Sub OnError(ByVal Sender As Object, ByVal e As EventArgs)
Dim objApp As HttpApplication
objApp = CType(Sender, HttpApplication)
Dim ctx As HttpContext = objApp.Context

ctx.Response.ClearContent()

ctx.Response.Write( "<font size='30' color='red'>unhandled error
occured</font>")

ctx.Response.End()

End Sub

In addition, you can also build a custom error page so that we can use
Server.Transfer to redirect the context to our custom error handling page.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)



--------------------
| Thread-Topic: question regarding this httpmodule
| thread-index: AcWCCRfXvfZq231RTSGw1n1egq22Aw==
| X-WBNR-Posting-Host: 64.180.224.155
| From: =?Utf-8?B?U2FtdWVs?= <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: RE: question regarding this httpmodule
| Date: Wed, 6 Jul 2005 02:00:01 -0700
| Lines: 6
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.general
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.general:45503
| X-Tomcat-NG: microsoft.public.dotnet.general
|
| Thanks Kevin, the module now compiles correctly. however, I am still not
too
| sure how to use it to response.write out an error message with the
module. I
| included a reference in web.config, and created a page with a unhandled
| runtime error in it. No matter what error I have, it just does not
display
| the error message based on what I specified in the module. All it shows
is
| the standard ASP.NET error page.
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top