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.

Related podcasts

Events coming up

  • Mar 15

    DevWeek 2010

    London, United Kingdom

    DevWeek is Europe’s leading independent conference for software developers, database professionals and IT architects, and features expert speakers on a wide range of topics, including .NET 4.0, Silverlight 3, WCF 4, Visual Studio 2010, REST, Windows Workflow 4, Thread Synchronization, ASP.NET 4.0, SQL Server 2008 R2, LINQ, Unit Testing, CLR & C# 4.0, .NET Patterns, WPF 4, F#, Windows Azure, ADO.NET, Entity Framework, Debugging, T-SQL Tips & Tricks, and more.

We'd love to hear what you think! Submit ideas or give us feedback