Basically HTTP handlers are special types of ASP.NET files where you provide content to user by manipulating HttpResponse object. Though the same functionality can be achieved with regular aspx page in this scenario HTTP handler is more suitable because it HTTP handlers are more lightweight and it takes less time for ASP.NET engine to process them.
An ASP.NET HTTP handler is the process (frequently referred to as the "endpoint") that runs in response to a request made to an ASP.NET Web application. The most common handler is an ASP.NET page handler that processes .aspx files. When users request an .aspx file, the request is processed by the page through the page handler. You can create your own HTTP handlers that render custom output to the browser.
The IsReusable property specifies whether the IHttpHandlerFactory object (the object that actually calls the appropriate handler) can put the handler in a pool and reuse it to increase performance. If the handler cannot be pooled, the factory must create a new instance of the handler every time that the handler is needed.
public bool IsReusable
{
get
{
return false;
}
}
The ProcessRequest method is responsible for processing individual HTTP requests. In this method, you write the code that produces the output for the handler.
HTTP handlers have access to the application context. This includes the requesting user's identity (if known), application state, and session information. When an HTTP handler is requested, ASP.NET calls the ProcessRequest method of the appropriate handler. The code that you write in the handler's ProcessRequest method creates a response, which is sent back to the requesting browser.
//...
// Add necessary namespace to the top
// as we are working with file system
using System.IO;
public void ProcessRequest(HttpContext context)
{
// You should validate query string here to make sure
// it contains necessary information
if (context.Request.QueryString.Count <= 0)
{
// could redirect to error page here
}
string file = context.Request.QueryString["FileName"];
if (string.IsNullOrEmpty(file))
{
// could redirect to error page here
}
// Combine file name with folder name
string filePath = context.Server.MapPath(string.Concat("~/Files/" + file));
// Check if requested file exists
if (!File.Exists(filePath))
{
// could redirect to error page here
}
// Always nice to know you're working with clear response object
// just in case
context.Response.Clear();
// This is the heart of your functionality. Browser read response's content type
// and decide whether they should show Save As.. dialog or show response in
// in it's window. Special value application/save forces browser to show Save As... dialog
context.Response.ContentType = "application/save";
// Browser use this to detect name of a file to be downloaded
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + file);
// Add file itself to response
context.Response.WriteFile(filePath);
// Flush our rendered response to browser
context.Response.Flush();
// We are done
context.Response.End();
}
<asp:HyperLink runat="server" NavigateUrl="~/FileDownload.ashx?FileName=template.txt" Text="Download template" />
<br />
<a href="FileDownload.ashx?FileName=template.txt">Download template</a>
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (2)
Commented:
A HUGE THANKS for this post.
Author
Commented:I'm very glad it helped you
R