使用System.Reflection加载ASP.NET 2.0 aspx页面?

我可以使用System.Reflection在另一个独立的aspx页面中加载一个独立的aspx页面吗?

我正在使用ASP.NET 2.0网站项目模型。

尝试使用BuildManager.CreateInstanceFromVirtualPath 。 样品用法:

Page p = BuildManager.CreateInstanceFromVirtualPath("~/Default.aspx", typeof(Page)) 

这回答了这个具体问题,但根据你的评论,我不确定这是你真正想要的。

不知道使用Reflection做这件事,这很可能,但你可以使用HttpContext.Server.Execute()将aspx或asp页面的输出捕获到字符串编写器。
我用它来渲染一些复杂的电子邮件模板,但不知道这是不是你想要的。

如果您的代码隐藏页面有UI.Page的inheritance类,则可以使用以下方式: 将CONTEXT设置为当前的http上下文

 Dim hndlr As IHttpHandler = PageParser.GetCompiledPageInstance("~/mypage.aspx", context.Server.MapPath("~/mypage.aspx"), CONTEXT) Dim ipage As DerivedPage = DirectCast(hndlr, DerivedPage) ipage.YourProperty= "Hello" ipage.DoIt() 

因此,您可以拥有强大的类型值,如果您要更改方法的符号,您将收到警告。

我实现了以下解决方案,这正是我想要做的:

 using System.Reflection; using System.Web.Compilation; Page p = BuildManager.CreateInstanceFromVirtualPath("~/mypage.aspx", typeof(Page)) as Page; MethodInfo MyMethod = p.GetType().GetMethod("MyMethod"); MyMethod.Invoke(p, null);