Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 582
chrismay Avatar asked Oct 23 '14 14:10

chrismay


2 Answers

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.

like image 144
Patrick Hofman Avatar answered Sep 25 '22 01:09

Patrick Hofman


Well, an ASHX can be a bit more contained and specific... however, there is something to be said about having all your code and logic in your main application.

There is no technical reason to do one over the other, to my knowledge with MVC nowadays. With WebForms it was harder to stream large files, but with MVC you can do that pretty easily (so you don't have to load the whole file into memory first). Also, given modern Async methods you don't have to worry so much about tying up worker threads and what not for scalability.

It's really up to you. Even if you wanted to separate it into its own module, nowadays it might make more sense to make it an owin module rather than an ashx. It's more about how you want to design your app.

like image 34
Erik Funkenbusch Avatar answered Sep 23 '22 01:09

Erik Funkenbusch