SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ASP.NET / VB: Write output to both browser and file

    Using ASP.NET and VB, how can I write a webpage to a file as well as send it to a browser? In the example below, a file is created but it only contains the word "True" instead of the content of the page.

    Code:
    <%@ Page Language="VB" AutoEventWireup="false" %>
    <%
    Response.BufferOutput = true
    %>
    Hello
    <%
    Response.write("Earth")
    Dim outputData as String
    
    IO.File.WriteAllText(Server.MapPath("output.txt"), buffer.ToString())
    
    %>

  2. #2
    SitePoint Member
    Join Date
    Jan 2012
    Location
    Virginia
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I would create the webpage content in a StringBuilder and then display the page and output it to the text file as well. Does that make any sense?

  3. #3
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,810
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hknight View Post
    Using ASP.NET and VB, how can I write a webpage to a file as well as send it to a browser? In the example below, a file is created but it only contains the word "True" instead of the content of the page.

    Code:
    <%@ Page Language="VB" AutoEventWireup="false" %>
    <%
    Response.BufferOutput = true
    %>
    Hello
    <%
    Response.write("Earth")
    Dim outputData as String
    
    IO.File.WriteAllText(Server.MapPath("output.txt"), buffer.ToString())
    
    %>
    What is your end goal? Are you trying to create a cache? Why do you need to write the output to a file?

    If for caching, just enable Page Caching

    If you are trying to generate static HTML files, then a stringbuilder approach "would" work, but it is ugly, and will give you a terrible code behind. A better approach in my opinion is to write another utility that invokes a WebRequest (pass the URL of the page you want to save), and write the Response to a file.

    This requires two applications, one that handles the saving of the page to file and the application that generates the file output you want to save, but it is much cleaner and leans more towards SOA (Service Oriented Architecture)
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cpradio, thank you for your thoughtful response. I want to do more than just caching. I want to use REGEX to modify the content of the page, as demonstrated in my code below.

    Because I want to modify the HTML with REGEX, enabling Page Caching will not do the trick. A stringbuilder approach might be ugly but it is what I need.

    This would be very easy to do with PHP:
    $outputData = ob_get_contents();

    Is there any way at all to do this with ASP.NET / VB?

    Thanks for the help.
    Code:
    <%@ Page Language="VB" AutoEventWireup="false" %>
    <%
    Response.BufferOutput = true
    %>
    Hello
    <%
    Response.write("Earth")
    Dim outputData as String
    
    outputData = buffer.ToString() ' THIS LINE DOES NOT WORK!  IT IS THE LINE THAT NEEDS FIXING!!!!!!!!!!!
    
    outputData = System.Text.RegularExpressions.Regex.Replace(outputData.Trim(), "(?=\s\s)\s*?(\n)\s*|(\s)\s+", "$1$2")
    
    Response.Clear()
    
    Response.write(outputData)
    
    IO.File.WriteAllText(Server.MapPath("output.txt"), outputData)
    
    %>

  5. #5
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,576
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Why are you trying to modify the content with a regex? There are almost certainly better approaches here.

    If you really need to do it that way, you should look at using the UrlRewriter -- it has the capability to modify output in responses in some ways.

    If you really need to do it internally, you will need to capture the output stream very late in the rendering cycle. Note that by pulling the whole string out to use a regex you will murder performance dead.

  6. #6
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still, is there any way to do this?

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,810
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    I agree with @wwb_99 ;

    It looks like your regular expression is looking for newlines and spaces and trying to trim them down. There are far better approaches indeed, such as, performing that step in the code behind, or the original data that is driving those oddities.

    However, to answer your question, it does not work because you are capturing the output in the wrong event (you are in the Render event). You really need to capture it in the Page.Unload event (most likely, but that may be too late, as the Response gets cleared in this event)

    I strongly encourage that you read about the page life cycle of .NET so you get a better understanding of how it operates.
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  8. #8
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,576
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Could it be done? Yes. Is it completely pointless? Yes. Is it potentially harmful to your app's performance? Yes. ASP.NET webforms does lots of tricks under the hood to stream the content down the wire very, very fast. Stopping that process to capture the results will get pretty ugly pretty fast.

    The only reason I can think of this mattering for is for compression. If so there are better solutions, like gzip which is baked into IIS and is merely a checkbox to turn on. If you really feel the need to do this see this article -- http://www.codeproject.com/Articles/...tput-using-Res ; the short version is you want to use a response filter. This is not a problem for the Page instance to even be aware of.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •