MVC缓存数据库图像 [英] MVC caching database images

查看:91
本文介绍了MVC缓存数据库图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创造与MVC 4.网站对于项目的要求,图像存储在数据库中。我有我绑定在我所伴随的故事画面的ID模型的视图,然后我得到的图片:

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:

查看:

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

控制器:

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

有了这个和Chrome的检查它,我注意到,当我重新加载页面,它不会从缓存中加载图像,就像从文件系统加载的其他文件,名为.css或其他图片。

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.

推荐答案

您可以装饰用<一个你的 ShowImagen 控制器操作href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.outputcacheattribute%28v=vs.108%29.aspx\"><$c$c>[OutputCache]属性:

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

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

这篇关于MVC缓存数据库图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆