RemoveOutputCacheItem不起作用

时间:2014-01-31 09:35:18

标签: asp.net asp.net-mvc outputcache

我有ActionResult返回文件结果:

[OutputCache(VaryByParam = "document_id;size", Duration = 3 * 60 * 60, Location = OutputCacheLocation.Server)]
public ActionResult GetDocumentThumbnail(Guid document_id, int size)
{
    byte[] thumbnail = null;
    switch (size)
    {
        case 100:
            thumbnail =
                (from a in _unitOfWork.Documents
                    where a.Id == document_id
                    select a.Thumbnails.Thumbnail_100).First();
            break;

        case 25:
            thumbnail =
                (from a in _unitOfWork.Documents
                    where a.Id == document_id
                    select a.Thumbnails.Thumbnail_25).First();
            break;
    }
    return File(thumbnail, "image/png");
}

Action正确缓存,因此在第一次加载后,所有其他请求都不再进入Action正文。

当我尝试删除特定文档的缓存时,问题就出现了:

我有这个功能,我打电话但什么也没做(当我再次请求时,文档缩略图仍然被缓存)

private void RemoveDocumentThumbnailCache(Guid document_Id)
{
    HttpResponse.RemoveOutputCacheItem("/DocumentThumbnail/" + document_Id + "/");
}

我也尝试了this solution但没有结果:我添加了自定义路由,因此路径不包含查询字符串参数。不工作

routes.MapRoute(
    name: "DocumentThumbnail",
    url: "DocumentThumbnail/{document_id}/{size}",
    defaults: new { controller = "Home", action = "GetDocumentThumbnail" }
);

我做错了什么?

1 个答案:

答案 0 :(得分:0)

RemoveOutputCacheItem必须是完整的相对网址。 您只能在/DocumentThumbnail/{document_Id}

时传递/DocumentThumbnail/{document_Id}/{size}
private void RemoveDocumentThumbnailCache(Guid document_Id)
{
    foreach(var size in new[] { 100, 25 }) {
        var url = Url.Action("GetDocumentThumbnail", new { document_id = document_id, size = size });
        HttpResponse.RemoveOutputCacheItem(url);
    }
}