Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I output a string instead of a HTML page?
    text
    copied!<p>I want to be able to output a simple string as text rather than the HTML page when a set of criteria are met. I am currently using:</p> <pre><code>Protected Sub OutputString(ByVal Str As String) Dim bytes() As Byte bytes = System.Text.Encoding.UTF8.GetBytes(Str) Response.ContentType = "text/plain" Response.OutputStream.Write(bytes, 0, bytes.Length) Response.End() End Sub </code></pre> <p>This works great and does exactly what I want, however, the Response.End() generates exceptions and (I believe) should be avoided where possible.</p> <p>I suppose I could use a separate "empty" page for this code but its more of a work around than a solution. Also, someone said something about over riding the HTML output behaviour (can't remember where now).</p> <p>I would be very grateful if someone could tell me the best method.</p> <p><strong>UPDATE:</strong> I found this link : <a href="http://web.archive.org/web/20101224113858/http://www.c6software.com/codesolutions/dotnet/threadabortexception.aspx" rel="nofollow">Response.Redirect(url) ThreadAbortException Solution</a></p> <p>It recommends using HttpContext.Current.ApplicationInstance.CompleteRequest() and over riding the functions that render the page:</p> <pre><code>Dim DoNotOutputHTML As Boolean = False Protected Overrides Sub RaisePostBackEvent(sourceControl As IPostBackEventHandler, eventArgument As String) If DoNotOutputHTML = False Then MyBase.RaisePostBackEvent(sourceControl, eventArgument) End If End Sub Protected Overrides Sub Render(writer As HtmlTextWriter) If DoNotOutputHTML = False Then MyBase.Render(writer) End If End Sub </code></pre> <p>You can then choose whether or not to output your html page by setting the variable DoNotOutputHTML.</p> <p>The only problem I have with this is that any code after the CompleteRequest() still executes. I can work around this by restructuring my code although it would be neater (less if/else blocks) if I didn't have to, like I don't with response.end()</p> <p>Any suggestions?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload