从内联代码调用原始 Page_Load 函数

发布于 2024-11-19 02:07:18 字数 478 浏览 9 评论 0原文

我喜欢对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暗喜 2024-11-26 02:07:18

asp.net 模型是,.aspx 文件中声明的页面实际上是继承自 .aspx.cs 文件中声明的 System.Web.UI.Page 的类的后代类。

因此,您的 Page_Load 方法被调用,因为它基本上隐藏了原始的 Page_Load 方法。按照这个逻辑,您可以这样做:

<script language="CS" runat="server"> 
void Page_Load(object sender, System.EventArgs e)  
{      
   base.Page_Load(sender, e);
   // do some stuff in addition to the original Page_Load method 
} 
</script> 

不存在可访问性问题,因为 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:

<script language="CS" runat="server"> 
void Page_Load(object sender, System.EventArgs e)  
{      
   base.Page_Load(sender, e);
   // do some stuff in addition to the original Page_Load method 
} 
</script> 

There are no accessibility issues because asp.net by default declares Page_Load and similar methods as protected so descendant classes can call them.

雾里花 2024-11-26 02:07:18

是的:

void Page_Load(object sender, System.EventArgs e) 
{
     // Do some things before calling the original Page_Load
     base.Page_Load(sender, e);
     // Do some things after calling the original Page_Load
}

它起作用的原因是 ASP.Net 框架在 .aspx 文件被编译成一个类的模型上运行,该类继承自代码隐藏文件中定义的类(实际上是由 继承页面标签上的属性)

<%@ Inherits="WebApplication1._Default" ... 

Yes it is:

void Page_Load(object sender, System.EventArgs e) 
{
     // Do some things before calling the original Page_Load
     base.Page_Load(sender, e);
     // Do some things after calling the original Page_Load
}

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)

<%@ Inherits="WebApplication1._Default" ... 
风透绣罗衣 2024-11-26 02:07:18

您还可以尝试使用 PreLoad 方法。这些在 Page_Load 之前被调用,并且可能是一种更简洁的处理方式。

You can also try to use PreLoad method. Those get called before Page_Load and might be a cleaner way to handle things.

淡紫姑娘! 2024-11-26 02:07:18

这对我有用。

<script language="CS" runat="server"> 
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        Response.Write("additional stuff");
    } 
</script>

This works for me.

<script language="CS" runat="server"> 
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        Response.Write("additional stuff");
    } 
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文