MVC 3 中忽略 OutputCache 属性

发布于 2025-01-04 07:09:44 字数 782 浏览 0 评论 0原文

因此,我在 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 的各种设置。当我更改任何这些设置时,响应标头似乎永远不会改变。

缓存控制:无缓存、无存储、必须重新验证

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

内容长度:11933

内容类型:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

日期:2012 年 2 月 9 日星期四 18:16:35 GMT

过期:-1

编译指示:无缓存

服务器:Microsoft-IIS/7.5

任何帮助将不胜感激。

So I am having an issue with IE 7 being able to download a file from an SSL site built in MVC 3. For IE 7 to be able to save a file from an SSL site, it must be cache-able.

The code for the method is:

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

This working in IE9, Chrome,Safari, and Firefox.
I have tried various settings for VaryByParam, Duration and NoStore. When ever I change any of those settings the response headers never seem to change.

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

Content-Disposition:attachment; filename=PersonalInfo-02092012.xlsx

Content-Length:11933

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

Date:Thu, 09 Feb 2012 18:16:35 GMT

Expires:-1

Pragma:no-cache

Server:Microsoft-IIS/7.5

Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三寸金莲 2025-01-11 07:09:44

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

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

为了简洁起见,对有问题的属性进行了修剪:

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);
    }
}

I solved this one myself but am leaving it out there so that it may be of use to someone else.

The problem was that a custom ActionFilterAttribute was manually setting the cache information and therefor the caching I was setting on the Action were being ignored.

The Attribute in question trimmed for brevity:

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文