简体   繁体   English

在另一个页面中呈现一个aspx页面

[英]rendering an aspx page in another one

In my web project's business object editor page, I'm sending a notification email to the administrator after an object insert or update. 在我的Web项目的业务对象编辑器页面中,我在对象插入或更新后向管理员发送通知电子邮件。 But instead of sending a plain text mail, i want to send the html output of another aspx page(Notification.aspx) i simply prepared for this purpose. 但是,我想发送另一个aspx页面(Notification.aspx)的html输出而不是发送纯文本邮件,我只是为此目的而准备。

First i thought, I can create an instance of Notification.aspx then use it's RenderControl method for getting the output. 首先我想,我可以创建一个Notification.aspx的实例,然后使用它的RenderControl方法来获取输出。

However, in the codebehind of Editor.aspx page, i can't even reach the Notification's reference to create a new instance. 但是,在Editor.aspx页面的代码隐藏中,我甚至无法达到Notification的创建新实例的引用。

I wonder what is the best practice for loading and rendering a page in another one... 我想知道在另一个页面中加载和呈现页面的最佳实践是什么...

Thanks. 谢谢。

You can render a page by doing this: 您可以通过执行以下操作来呈现页面:

StringWriter _writer = new StringWriter();
HttpContext.Current.Server.Execute("MyPage.aspx", _writer);

string html = _writer.ToString();

this is what you looking for: 这就是你要找的东西:

Type t = BuildManager.GetCompiledType("~/mypage.aspx");
Page p = (Page)Activator.CreateInstance(t);
p.ProcessRequest(HttpContext.Current);

from here use your imagination.... 从这里用你的想象力....

See this question/answer: Can I set up HTML/Email Templates in C# on ASP.NET? 看到这个问题/答案: 我可以在ASP.NET上的C#中设置HTML /电子邮件模板吗? . Mark Brackett has what you're looking for, though there's a lot of other helpful advice there as well. Mark Brackett有你想要的东西,尽管那里还有很多其他有用的建议。

The page class is instantiated by the ASP.NET runtime when a request is made. 在发出请求时,ASP.NET运行时会实例化页面类。 So you can make a request and get the capture the response: 因此,您可以发出请求并获取捕获响应:

using (WebClient client = new WebClient())
using (Stream stream = client.OpenRead("http://mysite.com/notification.aspx"))
using (StreamReader reader = new StreamReader(stream))
{
    var contents = reader.ReadToEnd();
}

Sounds tricky. 听起来很棘手。 Keep in mind the page will need an appropriate HttpContext as well, in order to render correctly. 请记住,页面也需要适当的HttpContext,以便正确呈现。

I would consider using a UserControl instead. 我会考虑使用UserControl。 These can be simply loaded and rendered with the Page.LoadControl() method. 可以使用Page.LoadControl()方法简单地加载和渲染它们。 With a bit of jiggery-pokery, you can keep it from rendering in the page while extracting it's HTML. 通过一些jiggery-pokery,您可以防止它在页面中呈现,同时提取它的HTML。

RenderControl won't work, because the Page won't go through it's lifecycle. RenderControl不起作用,因为Page不会经历它的生命周期。 I've used an HttpHandler and a Response.Filter to capture the stream in the past for a similar purpose. 为了类似的目的,我使用了一个HttpHandler和一个Response.Filter来捕获过去的流。 I've posted code over at the ASP.NET forums previously . 我以前在ASP.NET论坛上发布了代码。

Edit: If you need to modify page output, you should combine this with the Server.Execute overload pointed out by MartinNH. 编辑:如果需要修改页面输出,则应将其与MartinNH 指出Server.Execute重载相结合。 That would simplify the code, removing the Response.Filter and such. 这将简化代码,删除Response.Filter等。 If you just want the page output directly, MartinNH's method is very clean. 如果您只想直接输出页面,MartinNH的方法非常干净。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM