In the action of your controller, you need to execute an HTTP request to retrieve the image from the remote server:
public ActionResult Thumb(int id) { using (var client = new WebClient()) { byte[] image = client.DownloadData("http://cdn.foo.com/myimage.jpg"); return File(image, "image/jpg"); } }
and then:
<img src="@Url.Action("Thumb")" alt="" />
Obviously, the image is being downloaded twice. Once in the controller from the CDN and once from the client. This completely violates the purpose of this controller action, and you can directly reference the image from the CDN:
<img src="http://cdn.foo.com/myimage.jpg" alt="" />
This, obviously, assumes that the client has access to the CDN.
Of course, if your controllerβs action is more than just extracting the image from the CDN and transferring it to the client, for example, extracting the image from the CDN and resizing it, you should definitely take the first approach.
source share