1

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:

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

This works great and does exactly what I want, however, the Response.End() generates exceptions and (I believe) should be avoided where possible.

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).

I would be very grateful if someone could tell me the best method.

UPDATE: I found this link : Response.Redirect(url) ThreadAbortException Solution

It recommends using HttpContext.Current.ApplicationInstance.CompleteRequest() and over riding the functions that render the page:

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

You can then choose whether or not to output your html page by setting the variable DoNotOutputHTML.

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()

Any suggestions?

4

2 回答 2

0

Basically you need to set the response type to text, and remove all HTML from the page.

于 2012-07-02T13:53:12.473 回答
0

最后,我只是使用了 Response.End() 并忽略了异常。似乎工作正常。

于 2012-10-23T17:08:07.370 回答