Is Response.End() considered harmful?

前端 未结 9 772
陌清茗
陌清茗 2020-11-22 01:01

This KB Article says that ASP.NET\'s Response.End() aborts a thread.

Reflector shows that it looks like this:

public void End()
{
            


        
9条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 01:36

    This question appears near the top of all google searches for information on response.end so for other searches like myself who wish to post CSV/XML/PDF etc in response to an event without rendering the entire ASPX page, this is how I do it. (overriding the render methods is overly complex for such a simple task IMO)

    // Add headers for a csv file or whatever
    Response.ContentType = "text/csv"
    Response.AddHeader("Content-Disposition", "attachment;filename=report.csv")
    Response.AddHeader("Pragma", "no-cache")
    Response.AddHeader("Cache-Control", "no-cache")
    
    // Write the data as binary from a unicode string
    Dim buffer As Byte()
    buffer = System.Text.Encoding.Unicode.GetBytes(csv)
    Response.BinaryWrite(buffer)
    
    // Sends the response buffer
    Response.Flush()
    
    // Prevents any other content from being sent to the browser
    Response.SuppressContent = True
    
    // Directs the thread to finish, bypassing additional processing
    HttpContext.Current.ApplicationInstance.CompleteRequest()
    

提交回复
热议问题