Can an ASP.NET MVC controller return an Image? -
can create controller returns image asset?
i route logic through controller, whenever url such following requested:
www.mywebsite.com/resource/image/topbanner
the controller topbanner.png
, send image directly client.
i've seen examples of have create view - don't want use view. want controller.
is possible?
use base controllers file method.
public actionresult image(string id) { var dir = server.mappath("/images"); var path = path.combine(dir, id + ".jpg"); //validate path security or use other means generate path. return base.file(path, "image/jpeg"); }
as note, seems efficient. did test requested image through controller (http://localhost/mycontroller/image/myimage
) , through direct url (http://localhost/images/myimage.jpg
) , results were:
- mvc: 7.6 milliseconds per photo
- direct: 6.7 milliseconds per photo
note: average time of request. average calculated making thousands of requests on local machine, totals should not include network latency or bandwidth issues.
Comments
Post a Comment