How best to work with the Expires header in ASP.NET MVC?

我的未来我决定 提交于 2019-12-05 06:53:16
  1. No, you don't have to use this method. However, I think it is probably the best method to choose, because it makes the controller more testable and less web-aware. The alternative would be to set the header manually in the Controller, like this:

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

  2. Well, the OutputCache attribute controls when the action runs at all, and when it returns cached HTML instead. Expires tells the browser when to re-fetch the HTML. So I wouldn't call them mutually exclusive, but they are certainly two sides of the same coin, and you're right to think that you may not need both. I would suggest reviewing the HTTP spec to decide what is most appropriate for your application.

Response.Expires This property specifies the number of minutes before a page cached in the browser expires ie. if the user returns to the same page before the specified number of minutes the cached version of the page is displayed.

Response.ExpiresAbsolute Using this property we can set the date and/or time at which page cached in the browser expires.

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

It sounds like you just need to vary by user:

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

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

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