What are the benefits of an ASHX handler file in asp.net?

What are the benefits of using an ashx, or handler? Also, do I need them if I use MVC (and why not)?

Does the framework matter (2.0+)?

Why would I want to use a handler? I was recently recommended to use one for retrieving an image but I don't know why.

Thank you for your time.

Edit - is a handler faster?

Answers


Just a few examples:

  1. Dynamic image generation: You can write handlers that return data driven images by creating an ASHX handler that returns image data and then using that URL in your tags. e.g. <img alt="user's custom icon" src="Icon.ashx?username=bob"></img>

  2. Returning REST-based XML or JSON data to AJAX code on the client.

  3. Custom HTML: Return totally custom HTML for a page when the ASP.NET Web Forms or MVC framework is too restrictive

I believe this has been available since 1.0


The purpose of handlers in non-MVC projects is to provide some type of encoded response, outside of HTML. Typically, a handler would return XML (rss, RESTful, etc), JSON for jQuery or other Javascript, or sometimes just pure data such as file binary downloads. I've used handlers to even return special javascript to be excuted on the client, as a way of lazy-loading large binary or requirements on a "demand-only" approach. More or less, a handler would be used to return "anything but HTML".

In MVC, you would move away from handlers and utilize the Controller to return whatever data you like. So, in the method like:

mywebsite.com/restapi/content/56223

You RestfulContentController would have a method for Index(), that would NOT return a View(), but instead pure XML or JSON.

public class JSONContentController : Controller
{
  public JsonResult Index(int ContentID)
  {
    // get Content() by ContentID
    //

    // return a JSON version
    return Content().SerializeToJSON();
  }
}

They're very useful if your working in an environment where you do not have access to IIS but want to change things like far-future expiry response headers to optimize caching for files like css, images, JavaScript

For images you can do stuff like on the fly optimization so you can request images like image.jpg.ashx?w=180&quality=70 and then use the handler to deliver the image based on the settings passed in the querystring


aspx inherits page which implements IRequireSessionState. So if you call it via Ajax then asp.net needs to lock the session before further processing.

For ashx file it is stateless. Unless you inherit it from IRequireSessionState to manage state.

Use ashx for all Ajax calls and use aspx for purely asp.net page.


Need Your Help

How to debug a bug that can not be reproduced?

c# asp.net debugging

Now that the use of one of my asp.net apps has gone up significantly, two odd problems are occurring that are very infrequent, and that I cannot reproduce.

How validate max number of files before upload?

javascript jquery jquery-plugins

Hi I'm using the upload file plugin and I need to validate the number of files added before upload the file...Something like this