C# 在另一个页面中渲染一个 aspx 页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/647833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
rendering an aspx page in another one
提问by koraytaylan
In my web project's business object editor page, I'm sending a notification email to the administrator after an object insert or update. 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.
在我的 Web 项目的业务对象编辑器页面中,我在对象插入或更新后向管理员发送通知电子邮件。但不是发送纯文本邮件,我想发送另一个 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.
谢谢。
采纳答案by MartinHN
You can render a page by doing this:
您可以通过执行以下操作来呈现页面:
StringWriter _writer = new StringWriter();
HttpContext.Current.Server.Execute("MyPage.aspx", _writer);
string html = _writer.ToString();
回答by John Rudy
See this question/answer: Can I set up HTML/Email Templates in C# on ASP.NET?. Mark Brackett has what you're looking for, though there's a lot of other helpful advice there as well.
请参阅此问题/答案:我可以在 ASP.NET 上用 C# 设置 HTML/电子邮件模板吗?. Mark Brackett 有您要找的东西,但也有很多其他有用的建议。
回答by Darin Dimitrov
The page class is instantiated by the ASP.NET runtime when a request is made. So you can make a request and get the capture the response:
当发出请求时,页面类由 ASP.NET 运行时实例化。因此,您可以发出请求并获取响应:
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();
}
回答by Tor Haugen
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. These can be simply loaded and rendered with the Page.LoadControl()
method. With a bit of jiggery-pokery, you can keep it from rendering in the page while extracting it's HTML.
我会考虑使用 UserControl 代替。这些可以通过该Page.LoadControl()
方法简单地加载和呈现。使用一些jiggery-pokery,您可以在提取HTML 时阻止它在页面中呈现。
回答by Mark Brackett
RenderControl won't work, because the Page won't go through it's lifecycle. I've used an HttpHandler and a Response.Filter to capture the stream in the past for a similar purpose. I've posted code over at the ASP.NET forums previously.
RenderControl 将不起作用,因为 Page 不会经历它的生命周期。为了类似的目的,我过去曾使用 HttpHandler 和 Response.Filter 来捕获流。我以前在ASP.NET 论坛上发布过代码。
Edit: If you need to modify page output, you should combine this with the Server.Executeoverload pointed outby MartinNH. That would simplify the code, removing the Response.Filter and such. If you just want the page output directly, MartinNH's method is veryclean.
编辑:如果您需要修改页面输出,您应该将其与MartinNH指出的Server.Execute重载结合起来。这将简化代码,删除 Response.Filter 等。如果只是想直接输出页面,MartinNH的方法很干净。
回答by ygaradon
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....
从这里开始发挥你的想象力......