7

I want to be able to set a long expires time for certain items that a user downloads via GET request.

I want to say 'this is good for 10 minutes' (i.e. I want to set an Expires header for +10 minutes). The requests are fragments of HTML that are being displayed in the page via AJAX and they're good for the user's session. I don't want to go back to the server and get a 304 if they need them again - I want the browser cache to instantly give me the same item.

I found an article which is almost a year old about MVC Action filter caching and compression. This creates a custom ActionFilter to change the expires header. I'm already using the compression filter which works great for some custom css I am generating (94% compression rate!).

I have two main concerns :

1) Do I really have to use this method. I'm fine with it if I do, but is there really no functionality in MVC or the OutputCache functionality to do this for me? In 'traditional' ASP.NET I've always just set the Expires header manually, but we cant do that anymore - at least not in the controller.

2) If I do use this filter method - is it going to interfere with the OutputCache policy at all - which I want to be able to control in web.config. I'm kind of thinking the two are mutually exclusive and you wouldn't want both - but I'm not completely sure.

4

3 回答 3

3
  1. 不,您不必使用此方法。但是,我认为这可能是最好的选择方法,因为它使控制器更具可测试性和更少的网络感知。另一种方法是在 Controller 中手动设置标头,如下所示:

    Response.AddHeader("Expires", "Thu, 01 Dec 1994 16:00:00 GMT");

  2. 好吧,OutputCache 属性控制操作何时运行,以及何时返回缓存的 HTML。Expires 告诉浏览器何时重新获取 HTML。所以我不会说它们是相互排斥的,但它们肯定是同一枚硬币的两个方面,你认为你可能不需要两者都是正确的。我建议查看HTTP 规范以确定最适合您的应用程序的内容。

于 2009-02-16T14:25:10.697 回答
2

Response.Expires 此属性指定缓存在浏览器中的页面过期前的分钟数,即。如果用户在指定的分钟数之前返回到同一页面,则显示该页面的缓存版本。

Response.ExpiresAbsolute 使用此属性,我们可以设置浏览器中缓存的页面过期的日期和/或时间。

http://forums.asp.net/t/1532229.aspx

于 2011-05-10T10:14:57.850 回答
0

听起来您只需要因用户而异:

http://aspadvice.com/blogs/ssmith/archive/2007/10/29/VaryByCustom-Caching-By-User.aspx

[OutputCache(Duration="10", VaryByCustom="username")]

全球.asax:

public override string GetVaryByCustomString(HttpContext context, string key)
{
    switch(key)
    {
        case "username":
            return context.User.Identity.Name;

        // Other VaryByCustom strategy implementations can go here.
    }

    return string.Empty;
}
于 2013-12-12T18:44:19.077 回答