Images via IIS or Controller ActionResults
I have an ASP.NET MVC2 application that is maintaining a library of about 10GB of images. Many of these are customized by the application. We had been storing these in SQL but for a number of reasons, we are moving our images to the file system.
The file system vs SQL issue aside, should I be serving up my images through ActionResults on Controllers or directly serving them up via IIS and a separate web site? The former offers a number of advantages such as control over caching and some dynamic redirection but I would assume directly accessing files via IIS will be more efficient. How big an efficiency hit will I take serving u开发者_如何转开发p the images myself?
Andrew,
My first suggestion is to look at SQL Server's Filestream type. It provides a nice balence between storing blobs in SQL vs. the file system, while allowing access from both locations. See this for more information.
For your other question, it's fairly simple to serve up images from an Action Method. Create a method with your needed parameters, and instead of return an ActionResult, return a FileResult. Then in that method, load the image and return it.
One note about this - you really want to use a stream if possible, otherwise your memory and latency is going to get hit. If you can use a stream (via streaming the file on disk or from SQL Server), then you can return a new FileStreamResult, passing in the stream and the mime type of the image (e.g., "image/png"). That will give you much better performance and latency.
In terms of caching, you can use MVCs caching attributes to handle the image caching however you need to.
Let me know if that makes sense.
Erick
The easiest way is to drop the files into a directory on the server and reference them the way you would any other file. File paths that exist take priority over MVC routes. I don't know that you would see any noticeable performance differences by serving them up through ActionResults if you would prefer to go that route.
I don't usually worry about it, it's perfectly viable to let IIS do the work. I guess it really depends on how much control you actually need over the response. If you only need to serve up the images when requested then I'd say just put them in a directory and let IIS serve them. You really shouldn't have to do any configuration of routes if you are worried about that because as I said, file paths that exist on the server take priority over matching mvc routes.
This best addresses my issues and questions
Can an ASP.NET MVC controller return an Image?
精彩评论