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 u
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
Just a few examples:
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>
Returning REST-based XML or JSON data to AJAX code on the client.
I believe this has been available since 1.0
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.
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();
}
}