【发布时间】:2010-09-16 05:02:04
【问题描述】:
我可以创建一个只返回图像资源的控制器吗?
每当请求如下 URL 时,我想通过控制器路由此逻辑:
www.mywebsite.com/resource/image/topbanner
控制器将查找topbanner.png 并将该图像直接发送回客户端。
我已经看到了必须创建视图的示例 - 我不想使用视图。我想只用控制器来完成这一切。
这可能吗?
【问题讨论】:
-
我在这里https://stackoverflow.com/questions/155906/creating-a-private-photo-gallery-using-aspnet-mvc 提出了类似的问题,并最终找到了一个很好的指南。我按照本指南创建了一个 ImageResult 类。 https://blog.maartenballiauw.be/post/2008/05/13/aspnet-mvc-custom-actionresult.html
-
如果您想修改图像,请use the ImageResizing.Net HttpModule 以获得最佳性能。如果不这样做,FilePathResult 只会增加百分之几的开销。 URL 重写增加了一点点。
-
为什么不使用 WebApi Controller 而不是 MVC?
ApiController class
标签: asp.net asp.net-mvc image controller