Manual File Downloads In ASP.NET

Wrapping Up

Another solution explained in this MSKB article is to use the HttpApplication.CompleteRequest method, instead of the Response.End method. Unfortunately, it doesn't help either - the "default" ASP.NET HTML code is still appended to the end of the dynamically generated content even if you call the CompleteRequest method.

Yet another solution that I came acrross on my quest was to completely remove all the HTML code off of the 'download' page (see the post at the end). Although it might work (I haven't tried), I don't like the solution because it feels like a hack (I mean, it is completely undocumented). There is no guarantee that future ASP.NET versions won't put some HTML content to the response buffers even if the associated aspx page is empty.

IHttpHandler is an interface defined by ASP.NET, which "Defines the contract that ASP.NET implements to synchronously process HTTP Web requests using custom HTTP handlers. Because I didn't want to change the Download's class name (the Shared CreateHyperlink method was referenced in several other places in the project), I've decided to "morph" the existing Download.aspx web page into a custom IHttpHandler:

I've excluded the Download.aspx file from the project and added a new Download class hosted in an ordinary Download.vb code file. I've added the Implements IHttpHandler clause and implemented the core IHttpHandler.ProcessRequest method by pasting the relevant code from the "old" Download.aspx.vb code-behind file.

In order to actually associate the Download.aspx URL with our custom HTTP handler, the following lines had to be added to the <system.web> section of the application's web.config file:

<httpHandlers>
<add verb="*" path="Download.aspx"
    type="NRozprava.Download, NRozprava"/>
</httpHandlers>

Now, whenever a browser requests the Download.aspx URL, the ASP.NET plumbing instantiates our Download class and calls the IHttpHandler.ProcessRequest method. What is written by the method implementation to the response buffer, exactly that is sent back to the browser - nothing more, nothing less!

The conclusion is simple: If you need to generate custom HTTP responses in your ASP.NET application, use the IHttpHandler-based technique. Forget about the other tricks; they're crude and simply not "politically correct".

You might also like...

Comments

About the author

Palo Mraz

Palo Mraz United States

I live in Slovakia with my wife, two sons (fulltime), one daughter (occasionally) and a dog. I've been doing Microsoft Windows development since 1988; primarily in VB. I'm a big fan of the MS .N...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“A computer lets you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila” - Mitch Ratcliffe