Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 当处理程序为MvcHandler时获取对服务器控件的引用_Asp.net_Asp.net Mvc_Ihttpmodule - Fatal编程技术网

Asp.net 当处理程序为MvcHandler时获取对服务器控件的引用

Asp.net 当处理程序为MvcHandler时获取对服务器控件的引用,asp.net,asp.net-mvc,ihttpmodule,Asp.net,Asp.net Mvc,Ihttpmodule,我正在开发一个httpmodule,如果不符合某些标准,可以在我的网站上隐藏某些内容。我的处理程序设置非常简单。以下是我的问题的相关部分: Public Interface IFeatureItem Property ID As Guid Sub FeatureItemPreRenderComplete(ByVal sender As Object, ByVal e As EventArgs) End Interface Public Class MyModule Imple

我正在开发一个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
公共接口IFeatureItem
属性ID作为Guid
子功能项PreRenderComplete(ByVal发送方作为对象,ByVal e作为事件参数)
端接口
公共类MyModule
实现System.Web.IHttpModule
Public Sub Init(ByVal上下文作为System.Web.HttpApplication)实现System.Web.IHttpModule.Init
AddHandler context.PreRequestHandlerExecute,应用程序的地址\u PreRequestHandlerExecute
端接头
私有子应用程序\u PreRequestHandlerExecute(ByVal源作为对象,ByVal e作为事件参数)
如果源的类型为HttpApplication,则
作为HttpApplication=源的Dim应用程序
如果type of Application.Context.Handler为第页,则
将页面设置为Page=Application.Context.Handler
AddHandler Page.PreRenderComplete,FeatureItemPreRenderComplete的地址
ElseIf type of Application.Context.Handler为System.Web.Mvc.MvcHandler,则
Dim MvcHandler作为System.Web.Mvc.MvcHandler=Application.Context.Handler
如果结束
如果结束
端接头
私有子功能ItemPreRenderComplete(ByVal源作为对象,ByVal e作为System.EventArgs)
将页面设置为页面=源
Dim存储库作为IFeatureRepository=GetRepository(Page.Application)保存支持的IFeature
Dim IFeatureItems作为IEnumerable(属于IFeatureItem)=GetIFeatureItems(页面)”遍历页面的控制树并返回IFeatureItems
对于IFeatureItems中的每个IFeatureItem
Dim FeatureEventArgs As FeatureEventArgs=新FeatureEventArgs(IFeatureItem.ID,FeatureAllowed(IFeatureItem.ID,Repository))
IFeatureItem.FeatureItemPreRenderComplete(Me、FeatureEventArgs)
下一个
端接头
末级
基本上是在页面对象上设置事件处理程序(如果处理程序是页面)。然后在PreRenderEvent中,我循环遍历页面上的所有IFeatureItems,并在IFeatureItem中调用一个方法。如果处理程序是一个页面,这将非常有用


此网站具有用于仪表板的mvc视图,还包含可能是IFeatureItem的webforms控件。我想做的是在这个视图中循环浏览webforms控件,并对它们进行与在普通页面上相同的处理,但我想不出一种方法来实现这一点,而且在谷歌上也没有运气。这在模块内是否可行?PreRequestHandlerExecute是设置我的事件处理程序的正确事件吗?

您试图从错误的扩展点执行此操作

在MVC中,继承自
Page
ViewPage
在虚拟
WebFormView
方法中呈现:
Render(ViewContext-ViewContext,TextWriter)

您的解决方案是重写此方法并在此处执行预渲染事件


为了更好地理解如何有效地执行此操作,我建议使用.NET Reflector查看
WebFormView
ViewPage
ViewUserControl
的源代码。基本上,WebFormView使用BuildManager基于ViewPath创建ViewUserControl或ViewPage。这两个类都源于
Control
,因此您可以直接从这里开始。

如果他想查看您提到的类(以及MVC框架的任何部分)的源代码,他应该下载源代码:我希望避免这样做,因为它不适合我的HttpModule。我想我可以创建一个新的pageBaseType和userControlBaseType来覆盖它们的OnPreRender子类。我希望能够从HttpModule执行此操作,以便进行最小的更改。使用HttpModule方法,它只会影响实现特定接口的对象。这种方法会更改我所有页面和控件的基类型,或者我必须进行一些巧妙的切换。我的问题是,要从MvcHandler对象中获取对这些ViewPage和ViewUserControl的引用,在ASP.NET WebForms中,HttpHandler类就是视图类(页面)。在MVC中,HttpHandler创建一个控制器,该控制器处理请求,然后使用控制器呈现各种可能的结果,这些结果可能会或可能不会导致从IHttpHandler派生的视图。似乎您正试图从MVC获取违反基本MVC体系结构的功能。然而,(我觉得这似乎有点不对劲),但要在HttpModule中保留此逻辑,您可以将委托事件处理程序存储在HttpContext.Items中,并在覆盖WebFormView时访问它。