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".
Comments