tags:

views:

2543

answers:

3

Hi, 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?

+1  A: 

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

Nick Allen - Tungle139
+1  A: 

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();
  }
}
eduncan911
+3  A: 

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

mjmarsh
Can't I do #1 without a handler? I have that now but the aspx page does binary write.http://stackoverflow.com/questions/612342/whats-the-best-way-to-display-an-image-from-a-sql-server-database-in-asp-net/612360#612360
johnny
@johnny: ASPX pages use more resources than an ASHX handler does.
R. Bemrose
Yes, aspx can do binary write but that is not what it is supposed to do. The primary purpose is to render HTML content. Handlers are specifically used for such tasks because you don't need to define a blank aspx file and code away in C#/VB in code-behind.
Chetan Sastry
Yes, you can do #1 without a handler. Actually, you can do everything without a handler and just use an .aspx page for everything. But, the purpose of handlers is to identify the seperation of the Html view from 'some other type of data'. Hence, my answer I posted seperately.
eduncan911
yes, but much less overhead for an ASHX. With the ASPX page you're unnecessarily executing all the ASP.NET pipleline stuff (View State, control tree, etc) that you'll never use.
mjmarsh