What is the best way to work with the Expires header in ASP.NET MVC?

I want to be able to set a long expiration time for certain items that the user loads with a GET request.

I want to say โ€œit's good for 10 minutesโ€ (i.e. I want to set the Expires header to +10 minutes). Requests are fragments of HTML that are displayed on the page through AJAX, and they are good for a user session. I do not want to return to the server and get 304 if they need them again. I want the browser cache to instantly give me the same element.

I found an article that almost a year ago on caching and compressing an MVC filter . This creates a custom ActionFilter to change the expires header. I already use a compression filter that works fine for some custom css that I generate (94% compression ratio!).

I have two main problems:

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

2) If I use this filter method - it will generally interfere with the OutputCache policy that I want to control in web.config. I kind of think that the two are mutually exclusive, and you don't need both, but I'm not quite sure.

+7
asp.net-mvc outputcache
source share
3 answers
  • No, you do not need to use this method. However, I think this is probably the best method to choose, because it makes the controller more testable and less knowledgeable on the Internet. An alternative would be to set the header manually in the controller, for example:

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

  • Well, the OutputCache attribute controls when an action is performed at all, and when it returns instead of cached HTML. Expires tells the browser when it re-selects HTML. Therefore, I would not call them mutually exclusive, but they are certainly two sides of the same coin, and you are right in thinking that you may not need both. I would suggest looking at the HTTP specification to decide what works best for your application.

+3
source share

Response.Expires This property indicates the number of minutes before the expiration of the cached page in the browser, i.e. if the user returns to the same page before the specified number of minutes, a cached version of the page will be displayed.

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

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

+2
source share

It looks like you just need to change from 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; } 
0
source share

All Articles