MVC caching database images

2019-04-09 01:29发布

I am creating a website with MVC 4. For project requirements, the images are stored in database. I have a View that I bind the 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've noticed that when I reload the page, It not load the images from the cache, like other files as .css or other images loaded from file system.

Is there any way to make these images are cached? A greeting and thanks.

2条回答
疯言疯语
2楼-- · 2019-04-09 02:23

You could decorate your ShowImagen controller action with the [OutputCache] attribute:

[OutputCache(Duration = 3600, Location = OutputCacheLocation.Client, VaryByParam = "id")]
public ActionResult ShowImagen(int id)
{
    ...
}
查看更多
贼婆χ
3楼-- · 2019-04-09 02:24

Have you tried decorating your action method with OutputCache attribute?

查看更多
登录 后发表回答