I am working working on an httpmodule to hide certain content on my website if certain criteria are not met. My handler setup is pretty simple. Here are the relevant parts to my question:
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
Basically setting up event handlers on the page object if the handler is a page. Then in the PreRenderEvent I loop through all the IFeatureItems on the page and call a method in IFeatureItem. This works great if the handler is a page.
This site has an mvc view for a dashboard and also contains webforms controls that could be an IFeatureItem. What I want to do is loop through the webforms controls in this view and do the same processing on them as I would on a normal page, but I can't figure out a way to do so and have had no luck googling. Is this possible within a module? Is PreRequestHandlerExecute the right event to set up my event handlers?
You are trying to do this from the wrong extensibility point.
In MVC the
ViewPage
, which inherits fromPage
is rendered in the virtualWebFormView
method:Render(ViewContext viewContext, TextWriter writer)
.Your solution is to override this Method and execute your pre-render events here.
To get a better understanding of how to do this effectively, I would suggest reviewing the source for
WebFormView
,ViewPage
, andViewUserControl
with .NET Reflector. Basically, the WebFormView uses the BuildManager to create the ViewUserControl or ViewPage based on the ViewPath. Both of these classes derive fromControl
, so it should be straight forward for you from there.