简体   繁体   English

ASP.NET MVC2和浏览器缓存

[英]ASP.NET MVC2 and Browser Caching

I have a web application that fetches a lot of content via ajax. 我有一个Web应用程序,通过ajax获取大量内容。 For example when a user edits some data, the browser will send the changes using an ajax post and then do an ajax get to get fresh content and replace an existing div on the page with that content. 例如,当用户编辑某些数据时,浏览器将使用ajax帖子发送更改,然后执行ajax获取新内容并使用该内容替换页面上的现有div。 This was working just find with MVC1, but in MVC2 I would get inconsistent results. 这只是找到MVC1,但在MVC2中我会得到不一致的结果。

I've found that MVC1 by default included an Expires item in the response headers set to the current time, but in MVC2 the Expires header is missing. 我发现MVC1默认包含设置为当前时间的响应头中的Expires项,但在MVC2中,Expires头丢失了。 This is a problem with some browsers (IE8) actually using the cached version of the ajax get instead of the fresh version. 这是一些问题,一些浏览器(IE8)实际上使用缓存版本的ajax get而不是新版本。

To deal with the problem I created a simple ActionFilterAttribute that sets the reponse cache to NoCache (see below), which works, but it seems kind of sillly to decorate every controller with this attribute. 为了解决这个问题,我创建了一个简单的ActionFilterAttribute,它将响应缓存设置为NoCache(见下文),这是有效的,但用这个属性装饰每个控制器似乎很狡猾。 Is there a global way to set this for every controller? 是否有一种全局方式为每个控制器设置此功能?

Is this a bug in MVC2 and it really should be setting the expires on every ActionResult/view/page? 这是MVC2中的一个错误,它真的应该在每个ActionResult / view / page上设置过期吗? Don't most MVC programs deal with data entry where stale data is a very bad thing? 难道大多数MVC程序都不处理数据输入,而过时的数据是非常糟糕的吗?

Thanks 谢谢

Dan


public class ResponseNoCachingAttribute : ActionFilterAttribute
{
    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        base.OnResultExecuted(filterContext);

        filterContext.HttpContext.Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
    }
}

您是否尝试将以下属性添加到控制器中?

[OutputCache(Location = OutputCacheLocation.None)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM