2011-02-04 21 views
0

如果某些條件未得到滿足,我正在處理httpmodule以隱藏我網站上的某些內容。我的處理程序設置非常簡單。下面是相關的部分我的問題:當處理程序是MvcHandler時獲取對服務器控件的引用

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控件,並且像在普通頁面上那樣對它們進行相同的處理,但是我找不到一種方法來這樣做,並且沒有Google的運氣。這是可能的一個模塊內? PreRequestHandlerExecute是否有正確的事件來設置我的事件處理程序?

回答

1

您正試圖從錯誤的可擴展性來做到這一點。

在MVC中,繼承自PageViewPage在虛擬WebFormView方法中呈現:Render(ViewContext viewContext, TextWriter writer)

您的解決方案是重寫此方法並在此處執行預渲染事件。

爲了更好地理解如何有效地做到這一點,我建議使用.NET Reflector查看WebFormView,ViewPageViewUserControl的源代碼。基本上,WebFormView使用BuildManager根據ViewPath創建ViewUserControl或ViewPage。這兩個類都來自Control,所以它應該是直接從那裏你。

+0

如果他想看到你提到的類的來源(以及MVC框架的任何部分),他應該只需下載源代碼:http://aspnet.codeplex.com/releases/view/58781 – 2011-02-09 23:29:38

相關問題