Load an ASP.NET 2.0 aspx page using System.Reflection?

核能气质少年 提交于 2019-12-17 16:54:15

问题


Can I load an stand alone aspx page in another stand alone aspx page using System.Reflection?

I am using the ASP.NET 2.0 Web site project model.


回答1:


Try using BuildManager.CreateInstanceFromVirtualPath. Sample usage:

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

This answers this specific question, although, based on your comments, I'm not sure that this is what you really want.




回答2:


Don't know about doing it using Reflection, which may well be possible, but you can capture the output of an aspx or asp page to a string writer using HttpContext.Server.Execute().
I have used this for rendering some complex email templates, but don't know if that is quite what you are after.




回答3:


If you have a inherited class from UI.Page for your code behind page, you could use this way: set CONTEXT to your current http context

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()

So you can have strong typed values and, if you'll change the sign of a method you'll be warned.




回答4:


I implemented the following solution and it is what I exactly want to do:

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);


来源:https://stackoverflow.com/questions/376404/load-an-asp-net-2-0-aspx-page-using-system-reflection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!