0

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?

Dustin Hodges
  • 4,110
  • 3
  • 26
  • 41

1 Answers1

1

You are trying to do this from the wrong extensibility point.

In MVC the ViewPage, which inherits from Page is rendered in the virtual WebFormView 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, and ViewUserControl with .NET Reflector. Basically, the WebFormView uses the BuildManager to create the ViewUserControl or ViewPage based on the ViewPath. Both of these classes derive from Control, so it should be straight forward for you from there.

smartcaveman
  • 41,281
  • 29
  • 127
  • 212
  • If he wants to see the source for the classes you mention (and any portion of the MVC framework) he should just download the source: http://aspnet.codeplex.com/releases/view/58781 – Nathan Anderson Feb 09 '11 at 23:29
  • I was hoping to avoid having to do this because it doesn't really fit into my HttpModule. I guess I could create a new pageBaseType and userControlBaseType on overwrite their OnPreRender subs. I was hoping to be able to do this from an HttpModule in order to have minimal changes. With the HttpModule approach it would only effect objects that implement a specific interface. This approach would change the basetype of all my pages and controls or I would have to do some clever switching. – Dustin Hodges Feb 10 '11 at 23:50
  • My question really is there anyway to get references to these ViewPage and ViewUserControl from an MvcHandler object – Dustin Hodges Feb 10 '11 at 23:51
  • In ASP.NET WebForms, the HttpHandler class is the View class (a Page). In MVC, the HttpHandler creates a Controller which processes the request and then uses Controller to render a variety of possible results which may or may not result in a View that derives from IHttpHandler. It seems like you are trying to get a functionality from MVC that violates the fundamental MVC architecture. – smartcaveman Feb 11 '11 at 15:42
  • However, (this seems hacky to me), but what you could do, to keep this logic in your HttpModule is store the delegate EventHandler in your HttpContext.Items, and access it in your override of the WebFormView. – smartcaveman Feb 11 '11 at 15:44
  • The functionality that I am trying to get necessarily violates the MVC architecture because I am dealing with WebForm controls within MVC. I appreciate the answers and comments though. You have pointed me in a direction in which I can use – Dustin Hodges Feb 14 '11 at 18:55