0

I have this action method which creates thumbnails on the fly:

    public ActionResult Thumbnail(int imageId, int width, int height)
    {
        Image image = ImageManager.GetImage(imageId);
        string thumbnailPath;
        if (image.HasThumbnail(width, height))
        {
            thumbnailPath = image.GetThumbnailPath(width, height);
        }
        else
        {
            thumbnailPath = image.CreateThumbnail(width, height);
        }
        /*
        Here, I've done the business of thumbnail creation,
        now since it's only a static resource, I want to let IIS serve it.
        What should I do? Using HttpContext.RewritePaht() doesn't work, as 
        I have to return an ActionResult here.
        */
        return File(image.GetThumbnailPath(width, height), image.MimeType);
    }

And and example of the URL that invokes this method is:

/create-thumbnail/300x200/for-image/34

However, after doing thumbnail creation business in this method, I want to let IIS serve the thumbnail. What should I do? How can I return the control back to IIS?

Saeed Neamati
  • 33,583
  • 40
  • 128
  • 183
  • What exactly do you mean by returning control back to IIS to serve the thumbnail? you would like the user to see a file download prompt when they visit the url? – Jason Kulatunga Jun 03 '12 at 15:54
  • I want IIS to serve the file, instead of me returning a `FileResult`. That seems clear. – Saeed Neamati Jun 03 '12 at 16:17

1 Answers1

1

If the thumbnail has been created on the file system, you can try you can try using one of the following action result types to return it.

FileContentResult
FilePathResult
FileStreamResult

..edit.. Updating my response with a more relevant answer regarding output caching.

You will probably want to take a look at the Ouput Caching article on Asp.net

basically the premise is that each time an action is called in MVC it executes the whole function again, which would be a huge performance hit for something as simple as thumbnails. Instead if you decorate your action with OutputCache you can set a cache timer and increase your performance.

[OutputCache(Duration = int.MaxValue, VaryByParam = "id;param1;param2")]

VaryByParam documentation

Jason Kulatunga
  • 5,606
  • 1
  • 23
  • 50
  • Letting IIS serve the file, has some advantages, one of which is that it sets appropriate and efficient caching headers in the response. None of the above mentioned action results are the solution. They are all MVC handling creating the response and doing everything. – Saeed Neamati Jun 03 '12 at 16:22
  • thanks for your help, but `OutputCache` only holds a copy of the result of your action in your **RAM**. That means that every time browser sends you a request, and you simply give it the same thing (unless varied by parameters). Appropriate cache headers prevent browser from sending any request at all in the first place, or simply send something and get a simple 304 response header without body. – Saeed Neamati Jun 03 '12 at 17:06
  • You might want to take a look at setting the Location attribute to Location=OutputCacheLocation.ServerAndClient which should enable etag support. http://stackoverflow.com/questions/5031278/set-etag-for-fileresult-mvc-3 – Jason Kulatunga Jun 03 '12 at 17:23
  • I still have to show my gratitude for following my question dear @Json. However, I can completely create, edit and remove HTTP header fields in response. I have no problem with that. Imagine that somebody asks a mathematical question from your teacher, you simply jump in between to write what he asks, and then simply let the teacher again answer. In this analogy, teacher is IIS, you are MCV, and the person who has asked the question is the client ;). I want my teacher (IIS) to answer the question (HTTP request). – Saeed Neamati Jun 03 '12 at 18:31