Search code examples
vb.netasp.net-2.0

Serving a download worked under IIS6 and is working under ASP.NET Dev server, but not under IIS7/8


The page I am looking at serves a generated PDF file:

Dim Response As System.Web.HttpResponse = System.Web.HttpContext.Current.Response
Response.Clear()
Response.ClearHeaders()
Response.AddHeader("Content-Type", "binary/octet-stream")
Response.AddHeader("Content-Disposition", "attachment; filename=View.pdf; size = " & pdfBytes.Length.ToString())
Response.Flush()

Response.BinaryWrite(pdfBytes)
Response.Flush()
Response.End()

The master page that the link button serving a download is in has the following first lines:

<%@ Master Language="VB" AutoEventWireup="false" Inherits="App.WebGui.Nav" CodeBehind="Nav.master.vb" %>
<% Response.CacheControl = "no-cache"%>
<% Response.AddHeader("Pragma", "no-cache")%>

The 3d line causes an exception:

Server cannot append header after HTTP headers have been sent.

When debugging in the VS 2010 built in ASP.NET Dev server or running in the old IIS6 there is no exception.

What changes are required for this to work in IIS7?

Is it possible to determine programmatically that the headers have already been sent and conditionally skip the 3d line in that case?


Solution

  • Changed pipeline mode from Integrated to Classic on the application pool and recycled the pool. No more error.