如果不符合某些条件,我正在开发httpmodule以隐藏我网站上的某些内容。我的处理程序设置非常简单。以下是我的问题的相关部分:
Public Interface IFeatureItem
Property ID As Guid
Sub FeatureItemPreRenderComplete(ByVal sender As Object, ByVal e As EventArgs)
End Interface
Public Class MyModule
Implements System.Web.IHttpModule
Public Sub Init(ByVal context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init
AddHandler context.PreRequestHandlerExecute, AddressOf Application_PreRequestHandlerExecute
End Sub
Private Sub Application_PreRequestHandlerExecute(ByVal source As Object, ByVal e As EventArgs)
If TypeOf source Is HttpApplication Then
Dim Application As HttpApplication = source
If TypeOf Application.Context.Handler Is Page Then
Dim Page As Page = Application.Context.Handler
AddHandler Page.PreRenderComplete, AddressOf FeatureItemPreRenderComplete
ElseIf TypeOf Application.Context.Handler Is System.Web.Mvc.MvcHandler Then
Dim MvcHandler As System.Web.Mvc.MvcHandler = Application.Context.Handler
<What do I do here>
End If
End If
End Sub
Private Sub FeatureItemPreRenderComplete(ByVal source As Object, ByVal e As System.EventArgs)
Dim Page As Page = source
Dim Repository As IFeatureRepository = GetRepository(Page.Application) 'Holds supported IFeature
Dim IFeatureItems As IEnumerable(Of IFeatureItem) = GetIFeatureItems(Page) 'Goes through Page's control tree and returns IFeatureItems
For Each IFeatureItem In IFeatureItems
Dim FeatureEventArgs As FeatureEventArgs = New FeatureEventArgs(IFeatureItem.ID, FeatureAllowed(IFeatureItem.ID, Repository))
IFeatureItem.FeatureItemPreRenderComplete(Me, FeatureEventArgs)
Next
End Sub
<Irrelevant stuff removed>
End Class
如果处理程序是页面,则基本上在页面对象上设置事件处理程序。然后在PreRenderEvent中,我遍历页面上的所有IFeatureItems并在IFeatureItem中调用一个方法。如果处理程序是页面,这很有用。
此站点具有仪表板的mvc视图,还包含可以是IFeatureItem的webforms控件。我想要做的是循环浏览此视图中的webforms控件,并对它们执行与在普通页面上相同的处理,但我无法找到一种方法,并且没有运气谷歌搜索。这可能在模块中吗? PreRequestHandler是否正确设置我的事件处理程序?
答案 0 :(得分:1)
您正试图从错误的扩展点进行此操作。
在MVC中,继承自ViewPage
的{{1}}以虚拟Page
方法呈现:WebFormView
。
您的解决方案是覆盖此方法并在此处执行预渲染事件。
为了更好地了解如何有效地执行此操作,我建议使用.NET Reflector查看Render(ViewContext viewContext, TextWriter writer)
,WebFormView
和ViewPage
的来源。基本上,WebFormView使用BuildManager基于ViewPath创建ViewUserControl或ViewPage。这两个类都来自ViewUserControl
,因此从那里开始就应该是直截了当的。