【发布时间】:2014-10-22 04:55:15
【问题描述】:
可能不是一个很好的问题,有点菜鸟的问题,但无论如何......
我正在编写如下所示的 HttpModule 并在网上查看了许多示例,不幸的是它们都是用 C# 编写的。他们似乎都在使用 AddHandler 将方法与事件挂钩。
问题:使用下面的 WithEvents 与使用 AddHandler 有什么不同,尤其是在代码安全方面?
Imports System.Web
Imports System.Web.UI
Public Class MyModule1
Implements IHttpModule
Private WithEvents _context As HttpApplication
Private WithEvents _page As Page
' <summary>
' You will need to configure this module in the web.config file of your
' web and register it with IIS before being able to use it. For more information
' see the following link: http://go.microsoft.com/?linkid=8101007
' </summary>
#Region "IHttpModule Members"
Public Sub Dispose() Implements IHttpModule.Dispose
' Clean-up code here
End Sub
Public Sub Init(ByVal context As HttpApplication) Implements IHttpModule.Init
_context = context
End Sub
#End Region
Public Sub OnLogRequest(ByVal source As Object, ByVal e As EventArgs) Handles _context.LogRequest
' Handles the LogRequest event to provide a custom logging
' implementation for it
End Sub
Private Sub _context_PreRequestHandlerExecute(sender As Object, e As System.EventArgs) Handles _context.PreRequestHandlerExecute
Dim page As Page = TryCast(_context.Context.CurrentHandler, Page)
If Not page Is Nothing Then _page = page
End Sub
Private Sub _page_Init(sender As Object, e As System.EventArgs) Handles _page.Init
'Does this affect Inits coded elsewhere?
End Sub
End Class
【问题讨论】:
-
谢谢,这个问题很有帮助,但更多地关注“为什么这不起作用”,而不是明确的“差异是......”等。
标签: asp.net .net vb.net .net-3.5 asp.net-3.5