Home > ASP.NET > using httpHandler in asp.net 2.0

using httpHandler in asp.net 2.0

Sometime you don’t want to use the default ASP.NET handler and want to write your own. This can easy do just some line of code. For example i have a class which is used to get a file from database and response a “Save As” dialog so user can choose to download the content to client:

download.my

using System.Web;
namespace MyHandler
{
   public class Download
   {
      public void ProcessRequest (HttpContext context) {
         FileAttach file = new FileAttach();//Get id from the request
         file.ID = Int32.Parse(Request.QueryString["id"]);
         file.ConnectionString = ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString;
         // Start getting the file content from database
         file.GetFileContent();
         // Write out the attachmentServer.ScriptTimeout = 600;
         Response.Buffer = true;Response.Clear();
         Response.ContentType = "application/octet-stream";Response.AddHeader("Content-Disposition", "attachment; filename=\"" + file.FileName + "\";");
         Response.AddHeader("Content-Length", file.Content.Length.ToString());Response.BinaryWrite(file.Content);

         // End the response and send the output to clientResponse.End();
      }
      public bool IsReusable {
         get { return true; }
      }
   }
}

After you have created the file, then you have two choices to save this file. First you can make a Class Library project, add “download.my” file, compile it into an assmbly then from the web project chooose add reference to new dll . Second you can add “download.my” in the App_Code directory, this directory is a new concept in ASP.NET 2.0. Any file inside it’ll be dynamicly compiled into an .dll.

To use the handler, in Web.Config we must specify an introduction so ASP.NET can use to handle the request:

<httpHandlers>
<add verb="*" path="download.my" type="MyHandler.Download, App_Code" />
</httpHandlers>

After you have confiured all the above information. You can call the handler by using :

<a href="download.my?id=<%# Eval("Id")%>">aaaaaaaaa</a>

In the other hand, you can use a Generic Handler (.ashx) to write the content for our handler. if you use this file you need not to write anything in Web.Config. This file is default understood by asp.net as a handler, and the syntax is the same when calling

<a href="download.ashx?id=<%# Eval("Id")%>">aaaaaaaaa</a>

If you still want to use your class and using Generic Handler is the handler link you just change the information in the tag header

<%@ WebHandler Language="C#" Class="MyHandler.Download" %>

where MyHandler is the namespace and Download is class name

Advertisements
Categories: ASP.NET Tags:
  1. July 26, 2006 at 11:17 PM

    Nice blog. Please contact me at: http://www.dotnetslackers.com/contact.aspx about your blog.

    Thanks

  1. November 1, 2013 at 5:51 PM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: