O'Reilly logo

ASP.NET MVC 2 Cookbook by Richard Kimber, Andrew Siemer

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

A custom ActionResult to return an image

I think the title pretty much sums this one up! In this recipe, we are going to implement our own custom ActionResult, whose sole purpose in life is to take a path to an image on the file system and render it to the web page directly. This is useful when you need to render images that are outside your website or when you want to control access to premium content that you would prefer not be leached off your site.

There are many implementations to this recipe on the net. However, most of them involve a semi-messy approach requiring you to create a memory stream to your file, to determine the file's content type, and to access the code through some form of extension method. In this recipe, we will create ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required