Is there a recommended way to return an image using ASP.NET Web API

前端 未结 2 1912
死守一世寂寞
死守一世寂寞 2020-11-30 22:09

What is the best way to return an image with 2 parameters (x and y for resize).

For example

~/api/image12345/200/200

Will return a

2条回答
  •  庸人自扰
    2020-11-30 22:27

    Images are heavy. ASP.NET WebForms, HttpHandlers, MVC, and Web API all do a absolutely terrible job of serving static files. IIS does an extremely good job of that - Often 20-100x more efficiently.

    If you want to get good performance, do URL rewriting at the latest during PostAuthorizeRequest, so IIS can pick up and serve the file. Yes, this means HttpModule-level event handling.

    [Disclaimer: I'm the author of the following article and open-source project]

    If you're doing something dynamic with images, check out this article on image processing pitfalls to avoid, and consider taking a look at ImageResizer. It has excellent disk caching (which uses IIS static file handling), and is easy to connect to image generation of any kind. It has optional bindings for AForge, FreeImage, and WIC as well, if you need to get advanced.

提交回复
热议问题