HTTP Generic Handler Description

Description and samples
Table of Contents
Summary

Description of how to implement a custom HTTP handler with the built-in and generic type.

Description

An ASP.NET HTTP handler is the process executed in response to a request - ending in ‘.ashx‘ -  made to an ASP.NET Web application.

How it works

ASP.NET recognizes all requests ending in ‘.ashx‘ as requests for a custom HTTP handler without any need for explicit registering the handler class in the web.config file or IIS.

The generic HTTP Handler injects pre-processing logic based on:

  1. The HTTP verb sent on the request (i.e. method/action)

A new generic HTTP handler can be implemented in a separated DLL or as an application item in the ASP.NET App_Code directory.

Invocation

The URL of the request will alwats contains the “.ashx” extension, like:

  • http://localhost/training.ashx
  • http://localhost/training.ashx?name=jseixas

When to use it

An advantage of using generic handlers over “.aspx” pages is that generic handlers are less complex since a page always involve firing several events which is always more expensive.

Customs handlers should be used instead of web forms when:

  • No data rendering is involved;
  • XML, JSON, text, images or other type of data is expected as response for the request

Operations
Generic pre-compiled handler

The handler is created through the “Generic handler” template and therefore two files are created:
(1) “genericHander1.ashx”
(2) “genericHander1.ashx.cs”

where

- The handler is created within the web application “App_Code” directory;
- The handler is pre-compiled within the web application
- The handler is invoked as “http://localhost/genericHander1.ashx?name=jseixas”

How to do it
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Training.IIS
{
    public class genericHandler1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/jpeg";
            string name = "Anonymous";

            if (context.Request.QueryString["Name"] != null)
            {

                name = context.Request.QueryString["Name"];

            }

            Image img = ImageBuilder.CreateGreeting(name);

            img.Save(context.Response.OutputStream, ImageFormat.Jpeg);

        }

		public bool IsReusable { get { return false; } }
    }

	public static class ImageBuilder
	{
		public static Image CreateGreeting(string name)
		{
			Bitmap bmp = new Bitmap(640, 120, PixelFormat.Format32bppRgb);

			using (Graphics g = Graphics.FromImage(bmp))
			{
				g.FillRectangle(Brushes.White, new Rectangle(0, 0, 640, 120));

				g.DrawString(string.Format("Hello {0}!", name), new Font( "Arial", 28 ), Brushes.Black, new Point(10, 10));
			}

			return bmp;

	}
}
Generic just-in-time-compiled handler

The handler is created as a single file named “genericHander2.ashx”

and

- The handler is created within the web application “App_Code” directory;
- The handler is compiled at runtime when invoked by first time;
- The handler is invoked as “http://localhost/genericHander2.ashx?name=jseixas”

How to do it
<%@ WebHandler Language="C#" Class="Training.IIS.genericHandler2" %>

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Training.IIS
{
    public class genericHandler2 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/jpeg";
            string name = "Anonymous";

            if (context.Request.QueryString["Name"] != null)
            {

                name = context.Request.QueryString["Name"];

            }

            Image img = ImageBuilder.CreateGreeting(name);

            img.Save(context.Response.OutputStream, ImageFormat.Jpeg);

        }

        public bool IsReusable { get { return false; } }
    }

	public static class ImageBuilder
	{
		public static Image CreateGreeting(string name)
		{
			Bitmap bmp = new Bitmap(640, 120, PixelFormat.Format32bppRgb);

			using (Graphics g = Graphics.FromImage(bmp))
			{
				g.FillRectangle(Brushes.White, new Rectangle(0, 0, 640, 120));

				g.DrawString(string.Format("Hello {0}!", name), new Font( "Arial", 28 ), Brushes.Black, new Point(10, 10));
			}

			return bmp;

	}
}
Generic pre-compiled handler in a separated library

The handler is created and compiled within a separated DLL by implementing the “IHttpHandler” interface and named as “genericHander3.cs”

and

- The assembly is referenced by the web application;
- A second file named “genericHander3.ashx” is created within the web application “App_Code” directory;
- The handler is invoked as “http://localhost/genericHander3.ashx?name=jseixas”

How to do it
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections.Generic;
using System.Linq;
using System.Web;

    public class genericHandler3 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/jpeg";
            string name = "Anonymous";

            if (context.Request.QueryString["Name"] != null)
            {

                name = context.Request.QueryString["Name"];

            }

            Image img = ImageBuilder.CreateGreeting(name);

            img.Save(context.Response.OutputStream, ImageFormat.Jpeg);

        }

        public bool IsReusable { get { return false; } }
    }

	public static class ImageBuilder
	{
		public static Image CreateGreeting(string name)
		{
			Bitmap bmp = new Bitmap(640, 120, PixelFormat.Format32bppRgb);

			using (Graphics g = Graphics.FromImage(bmp))
			{
				g.FillRectangle(Brushes.White, new Rectangle(0, 0, 640, 120));

				g.DrawString(string.Format("Hello {0}!", name), new Font( "Arial", 28 ), Brushes.Black, new Point(10, 10));
			}

			return bmp;

	}
// file genericHandler3.ashx
<%@ WebHandler Language="C#" Class="genericHandler3" %>