OutputCache attribute being ignored in MVC 3

巧了我就是萌 提交于 2020-01-02 11:59:07

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/9217823/outputcache-attribute-being-ignored-in-mvc-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!