Reputation: 175
I am attempting to load a parsed aspx file into a string. The reason for this is because I have a page with a fair amount of html on it which also has sections that need to be processed in order to insert the correct information. This page then needs to be displayed as a confirmation page as well sent in a confirmation email when the user submits a form.
Displaying the confirmation page is simple enough, but my idea to prevent code duplication is to render the aspx file to a string variable to use as the content of the email as well.
I realize that I could add all the content as a huge multi-line string and load it onto the aspx page, and then into the email, but this solution feels messy to me and will require an application re-compile should I ever want to change the content.
Perhaps somebody can suggest a better way to achieve what I am attempting?
The code I have come up with is below, but I am nit sure if it is possible to somehow catch the output from ProcessRequest before it gets sent to the browser:
IHttpHandler handler = PageParser.GetCompiledPageInstance("/SubmitConfirmation.aspx?appid=" + this.Id, "SubmitConfirmation.aspx", HttpContext.Current);
handler.ProcessRequest(HttpContext.Current);
string str_out = ???;
Basically what I am trying to achieve is the same as the following PHP code would do:
<?php
ob_start();
include("SubmitConfirmation.php");
$str_out = ob_get_contents();
ob_end_clean();
?>
Any suggestions are welcome.
Upvotes: 2
Views: 1356
Reputation: 6528
You need to execute the page. THe simplest way would be:
var writer = new StringWriter();
Server.Execute("/SubmitConfirmation.aspx?appid=" + this.Id, writer );
string str_out = writer.GetStringBuilder().ToString();
Upvotes: 2
Reputation: 4051
You can try:
WebClient client = new WebClient();
string html = client.DownloadString(@"???"); // your site
Upvotes: 0
Reputation: 8359
Did you try
System.IO.StringWriter htmlStringWriter = new System.IO.StringWriter();
Server.Execute("Page.aspx", htmlStringWriter);
string htmlOutput = htmlStringWriter.GetStringBuilder().ToString();
found on Kiyoshi's blog. Tested it - and worked for me. See MSDN, too
Upvotes: 4