How to use your own IHttpHandlers without changing IIS settings

As I know there are a lot of developers that do not have a internet server with full access to the IIS (f.e. using a administrator website or remote desktop). You have to change the IIS settings for the new file extension.

Yesterday I found a good solution on how to use a special file extension without changing IIS settings or to put settings in the web.config.

After the .NET installation you will find some file extensions already in the list. One of them will be .ASHX (ASP.NET Handler). In this file you have to write the class where you have put your IHttpHandler:

<%@ WebHandler language="c#" codebehind="MyChart.aspx.cs" class="WebApplication1.MyChart" %>

As you can see in the above sample you can use codebehind like in webforms or webservices.

My IHttpHandler sample application will allow you to use a similar technic like the new .ASIX file extension in Microsoft .NET 2.0 (beta).

using System;
using
System.Drawing;
using
System.Drawing.Drawing2D;
using
System.Web;
namespace
WebApplication1
{
  public delegate void
RenderEventHandler(Graphics g, System.EventArgs e);

  public
class
MyHttpHandler : IHttpHandler
  {
    public event
RenderEventHandler Render;
    protected int
Width = 100;
    protected int Height = 100;

    public MyHttpHandler()
    {
      OnInit(
new
EventArgs());
    }

    protected virtual void OnInit(EventArgs e)
    {}

    public void ProcessRequest(HttpContext context)
    {
      Bitmap bmp =
new
Bitmap(Width, Height);
      Graphics g = Graphics.FromImage(bmp);
      g.FillRectangle(
new
SolidBrush(Color.White), 0, 0, bmp.Width, bmp.Height);

      if(Render != null)
      {
        Render(g, new
EventArgs());
      }

      bmp.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
      g.Dispose();
      bmp.Dispose();
      g =  null;
      bmp =
null;
    }

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

Now, you can draw a image directly in your codebehind file. As you can see in the following codebehind file for the .ASHX you have a Render event that will be called from the IHttpHandler.

using System;
using
System.Drawing;
using
System.Web;
namespace
WebApplication1
{
  public class
MyChart : MyHttpHandler
  {

  private void Page_Render(Graphics g, System.EventArgs e)
  {
    g.DrawLine(
new
Pen(Color.Black), 0, 0, 100, 100);
  }

  override protected void OnInit(EventArgs e)
  {
    InitializeComponent();
    base
.OnInit(e);
  }

  private void InitializeComponent()
  {
    this
.Width = 100;
    this
.Height = 100;

    this.Render += new RenderEventHandler(this.Page_Render);
  }
}

Based on my article Math Berther has build a second solution on how to use own HttpHandlers:
http://www.mattberther.com/2004/07/000510.html

Published Friday, July 09, 2004 10:07 PM by Michael Schwarz

Comments

# Design Patterns : Template Method

Friday, July 09, 2004 11:41 PM by TrackBack
Let's take a look at how we can refactor an existing chunk of code to take advantage of the Template Method design pattern. This pattern's intent is to provide a skeleton of an algorithm in a method, deferring some steps...

# A little different source code based on this article.

Friday, July 16, 2004 1:41 AM by TrackBack
A little different source code based on this article.

# public MattBerther : ISerializable &raquo; Blog Archive &raquo; Design Patterns : Template Method

PingBack from http://www.mattberther.com/?p=510