MVC Database Images

I am building a website with MVC 4. For project requirements, images are stored in a database. I have a view that I associate with a model in which I have the id of the picture accompanying the story, then I get the image:

View:

<img src='<%= Url.Action("ShowImagen", "Home", new {id = item.IdImagen}) %>' style="width: 300px;
                        height: 200px;" />

Controller:

public FileResult ShowImagen(int id)
    {
        if (id > 0)
        {
            var imageData = new NoticiaRepository().GetImagen(id);
            return File(imageData, "image/jpg");
        }
        else
        {
            return null;
        }           
    }

With this and checking it with Chrome, I noticed that when you reload the page, it does not load images from the cache, like other files in the form of .css or other images downloaded from the file system.

Can these images be cached? Greetings and thanks.

+5
source share
2 answers

You can decorate your action with a controller ShowImagenattribute [OutputCache]:

[OutputCache(Duration = 3600, Location = OutputCacheLocation.Client, VaryByParam = "id")]
public ActionResult ShowImagen(int id)
{
    ...
}
+11
source
0

All Articles