Library code snippets

How to write an ASHX file

If you develop with ASP.NET, you probably spend most of your time creating .aspx files with .cs files as code behind (or perhaps you use .ascx files for your controls -- and .asmx files for web services). But have you created a .ashx file (web handler). A web handler file works just like an aspx file except you are one step back away from the messy browser level where HTML and C# mix. One reason you would write an .ashx file instead of an .aspx file is that your output is not going to a browser but to an xml-consuming client of some kind. Working with .ashx keeps you away from all the browser technology you don't need in this case. Notice that you have to include the IsReusable property.

<% @ webhandler language="C#" class="AverageHandler" %>

using System;
using System.Web;

public class AverageHandler : IHttpHandler
{
   public bool IsReusable
   { get { return true; } }
   public void ProcessRequest(HttpContext ctx)
   {
       ctx.Response.Write("hello");
   }
}

Comments

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of How to write an ASHX file.

Leave a comment

Sign in or Join us (it's free).

Edward Tanguay Edward Tanguay updates his personal web site tanguay.info weekly with code, links, quotes and thoughts on web development. Sign up for the free newsletter.
AddThis

Related podcasts

Events coming up

  • Jul 7

    DTC 70-528 Session 7: Chapter 12

    Greenwood Village, United States

    Due to lack of interest of the 5th Monday meetup, we will continue as originally scheduled. The topic of the night will be "Chapter 12 - Creating ASP.NET Mobile Web Applications", taught by RJ Hatch. It is a fairly small chapter, so we can discuss other topics as well. Pizza and beverages will be provided on a donation basis.

Want to stay in touch with what's going on? Follow us on twitter!