Can I create a Controller that simply returns an image asset?
I would like to route this logic through a controller, whenever a URL such as the following is requested:
www.mywebsite.com/resource/image/topbanner
The controller will look up topbanner.png
and send that image directly back to the client.
I've seen examples of this where you have to create a View - I don't want to use a View. I want to do it all with just the Controller.
Is this possible?
You can create your own extension and do this way.
You could use the HttpContext.Response and directly write the content to it (WriteFile() might work for you) and then return ContentResult from your action instead of ActionResult.
Disclaimer: I have not tried this, it's based on looking at the available APIs. :-)
SomeHelper.EmptyImageResult()
should returnFileResult
with existing image (1x1 transparent, for example).This is easiest way if you have files stored on local drive. If files are
byte[]
orstream
- then useFileContentResult
orFileStreamResult
as Dylan suggested.