【问题标题】:OutputCache attribute being ignored in MVC 3在 MVC 3 中忽略 OutputCache 属性
【发布时间】:2012-02-09 19:59:47
【问题描述】:

所以我在 IE 7 能够从 MVC 3 中内置的 SSL 站点下载文件时遇到问题。要让 IE 7 能够从 SSL 站点保存文件,它必须是可缓存的。

方法的代码是:

[OutputCache(Location = OutputCacheLocation.ServerAndClient, Duration = 20, VaryByParam = "none", NoStore = true )]
public override FileContentResult Export(int? id, string extra)
{
...
return new FileContentResult(byte[], mimetype);
}

这适用于 IE9、Chrome、Safari 和 Firefox。 我尝试了 VaryByParam、Duration 和 NoStore 的各种设置。当我更改任何这些设置时,响应标头似乎永远不会改变。

Cache-Control:no-cache, no-store, must-revalidate

内容配置:附件;文件名=PersonalInfo-02092012.xlsx

内容长度:11933

Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

日期:格林威治标准时间 2012 年 2 月 9 日星期四 18:16:35

过期:-1

Pragma:no-cache

服务器:Microsoft-IIS/7.5

任何帮助将不胜感激。

【问题讨论】:

标签: asp.net-mvc-3 caching attributes


【解决方案1】:

我自己解决了这个问题,但我把它留在那里,以便对其他人有用。

问题是自定义 ActionFilterAttribute 手动设置缓存信息,因此我在 Action 上设置的缓存被忽略。

为简洁起见,对相关属性进行了修剪:

public class CustomAttributeName: ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var cache = filterContext.HttpContext.Response.Cache;
        cache.SetExpires(DateTime.UtcNow.AddDays(-1));
        cache.SetValidUntilExpires(false);
        cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
        cache.SetCacheability(HttpCacheability.NoCache);
        cache.SetNoStore();

        base.OnActionExecuting(filterContext);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-22
    • 1970-01-01
    • 1970-01-01
    • 2012-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多