Search code examples
asp.netasp.net-mvcashx

ASP.NET Handler (ashx) vs MVC Controller Action for downloading files


We have an application that uses webforms for some older web app pages, but also contains MVC functionality for some of the newer features. We have some new requirements for downloading files that will have to be processed on the server (not direct links to static files on the web server).

I haven't seen anything to indicate if there is a reason one should use an ASHX handler over just using an MVC controller and operating on the response object and returning EmptyResult() at the end of the action method.

Is there a best-practice for this with MVC? Should ASHX handlers be left for WebForms or is there some benefit they provide over using MVC for this type of file download feature?


Solution

  • The performance of HttpHandler is better since it is more bare-metal than MVC actions (just a few extra steps, but still).

    Besides that, I see no reason why you should choose one over the other for performance reasons. MVC has some nice features you might want to use, like caching and authorization attributes.

    If you choose to use MVC, use results that are specifically built for file handling, like FileStreamResult or FileContentResult.