从内联代码调用原始 Page_Load 函数
我喜欢对 ASPX 网站进行 Monkey 修补,以便可以向已编译程序集中的 Page_Load 方法添加内容。
我的第一个想法是将包含第二个 Page_Load 方法的脚本标记添加到 ASPX 文件中,如下所示:
<script language="CS" runat="server">
void Page_Load(object sender, System.EventArgs e)
{
// do some stuff in addition to the original Page_Load method
}
</script>
但看起来只有内联代码中的 Page_Load 方法才会被执行,而不是原始代码隐藏文件中的方法(在已编译的程序集)。
是否可以从我的内联代码中调用原始方法?或者是否有其他方法可以添加在内联代码中调用 Page_Load 方法后直接运行的内容,而无需修改现有程序集?
I like to Monkey patch a ASPX website so that I can add stuff to the Page_Load method within a compiled assembly.
My first thought was to add a script tag containing a second Page_Load method to the ASPX file like so:
<script language="CS" runat="server">
void Page_Load(object sender, System.EventArgs e)
{
// do some stuff in addition to the original Page_Load method
}
</script>
But it looks like only the Page_Load method from the inline code will be executed and not the one from the original code-behind file (within the compiled assembly).
Is it possible to call the original method from within my inline code? Or is there any other way to add stuff that should run directly after the Page_Load method was called from within inline code without modifying the existing assembly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
asp.net 模型是,.aspx 文件中声明的页面实际上是继承自 .aspx.cs 文件中声明的
System.Web.UI.Page
的类的后代类。因此,您的 Page_Load 方法被调用,因为它基本上隐藏了原始的 Page_Load 方法。按照这个逻辑,您可以这样做:
不存在可访问性问题,因为 ASP.NET 默认情况下将 Page_Load 和类似方法声明为
protected
,以便后代类可以调用它们。The asp.net model is that the page declared in the .aspx file is actally a descendant class from the class that inherits from
System.Web.UI.Page
declared in the .aspx.cs file.So your Page_Load method is called because it basically hides the original Page_Load method. Following that logic, you could do:
There are no accessibility issues because asp.net by default declares Page_Load and similar methods as
protected
so descendant classes can call them.是的:
它起作用的原因是 ASP.Net 框架在 .aspx 文件被编译成一个类的模型上运行,该类继承自代码隐藏文件中定义的类(实际上是由
继承页面标签上的
属性)Yes it is:
The reason this works is that the ASP.Net framework operates on the model where the .aspx file gets compiled up into a class that inherits from the class defined in your code behind file (actually the class defined by the
Inherits
attribute on the page tag)您还可以尝试使用
PreLoad
方法。这些在Page_Load
之前被调用,并且可能是一种更简洁的处理方式。You can also try to use
PreLoad
method. Those get called beforePage_Load
and might be a cleaner way to handle things.这对我有用。
This works for me.