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"];
context.Response.Write(parameter);
}
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.

February 13, 2009 at 11:14 am
Brian, great entry! Also take a look at my article:
http://sitecore.alexiasoft.nl/2007/09/13/adding-your-own-httphandler-to-sitecore-53/
February 24, 2011 at 2:09 pm
Brian,
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.
February 25, 2011 at 9:20 am
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: http://www.dyrlev.dk/.
In this website I placed a Httphandler in /modules/mediamanager/media.ashx. I can now call it from http://www.dyrlev.dk/modules/mediamanager/media.ashx, allowing me to resize any image within my Umbraco solution by sending the file to resize and the size to return: http://www.dyrlev.dk/modules/mediamanager/media.ashx?file=/media/399/dyrlev (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: http://msdn.microsoft.com/en-us/library/46c5ddfy.aspx
February 28, 2011 at 12:01 pm
[...] Top Posts Using UserControls instead of XSLT in Sitecore projectsMultiple languages in SitecoreGSA (Google Search Appliance) Suggest using C# and jQuerySetting up Security on Languages in Sitecore 6The .ashx extension – Writing your own HttpHandler [...]
March 1, 2011 at 1:40 am
[...] documents, …) in its own database rather than on disk. All assets are then streamed using an HttpHandler. All image links goes to an *.ashx handler which then returns the image. You can apply parameters [...]
June 7, 2011 at 8:56 am
Can anyone help me, I want to display the data with different font when the file is exported. I tried with :
str2.Append(“”);
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