The .ashx extension – Writing your own HttpHandler

Have you noticed how Sitecore serves images using an .ashx extension? A .ashx file is a HttpHandler. A HttpHandler is kind of a lightweight aspx page, as the HttpHandler only deals with the HttpContext – for example there is no page information. This makes it a great tool for implementing providers for images, xml, rss feeds or other stuff that can be generated using parameters only.

The .ashx file implements the System.Web.IHttpHandler (or the System.Web.IHttpAcynchandler for async calls – I’ll only show the first one), which contains only 1 property (IsReusable) and one function (ProcessRequest()). The ProcessRequest() function have one parameter only, the HttpContext to read and write data to.

My Visual Studio have no default creation of .ashx extensions, so I’ll have to make one manually. I create a new file with the .ashx extension and adds the following line:

<% @ WebHandler language="C#" class="MyNamespace.MyClass" codebehind="mycodebehind.cs" %>

Then I have to create a mycodebehind.cs file and add a class called MyNamespace.MyClass. This class should implement the IHttpHandler interface:

using System.Web;

namespace MyNamespace
  public class MyClass : IHttpHandler
    public bool IsReusable
      get { return true; }

    public void ProcessRequest(HttpContext context)
      // Here goes all of my code

Now my ProcessRequest can serve anything I like. I can reteieve paramaters from the Request and write to the Response:

public void ProcessRequest(HttpContext context)
  string parameter = context.Request.Params["myparameter"];

Or I can output a file (like Sitecore does):

public void ProcessRequest(HttpContext context)
  System.IO.MemoryStream ms = new System.IO.MemoryStream();
  Image image = SomeFunctionGeneratingAnImageForme();
  image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
  context.Response.ContentType = "image/jpeg";
  context.Response.OutputStream.Write(ms.GetBuffer(), 0, Convert.ToInt32(ms.Length));

I’ve noticed how Sitecore registers theit HttpHandlers in the web.config. You really don’t have to do that (even if Microsoft says so). You can call them directly. But if you wish to do so, you should read this article on how to register a HttpHandler.

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in General .NET and tagged , . Bookmark the permalink.

9 Responses to The .ashx extension – Writing your own HttpHandler

  1. Josh Wheelock says:


    thanks for this post. What directory would you save th physical file to? Also, what URL would then be used to access the HttpHandler?

    Thanks in advance for your response.

  2. briancaos says:

    You can place your .ashx file anywhere you want to.

    Take a look at this (Umbraco) web site I did for a friend of mine:
    In this website I placed a Httphandler in /modules/mediamanager/media.ashx. I can now call it from, allowing me to resize any image within my Umbraco solution by sending the file to resize and the size to return: (24)-2.jpg&width=450

    The other (and more correct) solution is to register the HttpHandler, allowing the HttpHandler to be called from a certain path and beyond, or from a specific file extension. See how to do it here:

  3. Pingback: Sitecore Image Parameters « Brian Pedersen’s Sitecore and .NET Blog

  4. Pingback: Sitecore Image Parameters | CMS News Today

  5. Pravaneeth says:

    Can anyone help me, I want to display the data with different font when the file is exported. I tried with :

    but it is not applying that font when I open, by default it is taking Times New Roman. In MS Word by default ” Nudi Akshar ” is there but it is not applying for exported file

  6. owasp says:

    A little nitpicky, but your first example is a textbook XSS vulnerability.

  7. Pingback: Stream Sitecore media items to HttpResponse | Brian Pedersen's Sitecore and .NET Blog

  8. Pingback: URL Rewrite and Sitecore | Brian Pedersen's Sitecore and .NET Blog

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.