Click here to Skip to main content
15,886,798 members
Articles / Programming Languages / Visual Basic
Tip/Trick

iOS6 Cached POST

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
19 Nov 2012CPOL1 min read 9.3K   3  
How to resolve cached post problem with iPhone/iPad with iOS6 update.

Introduction 

Today, as webmaster of www.doblered.net (a tennis events related site) I've received an email saying that some information is not updated on iPhone/iPad. On PC's and Mac's all works fine. Searching in google, I've found this question in a forum: http://stackoverflow.com/questions/12506897/is-safari-on-ios-6-caching-ajax-results. So, working in a solution to this problem, here is my tip, that worked for me on ASP.NET with MVC3.

Background 

Solutions given in the thread implies to alter all my actions affected in many of my controllers, so I need a global solution. The GlobalFilters class is very useful to acomplish this.

Using the code

The first thing to do is create a class inherited from ActionFilterAttribute class with this code:
VB.NET
Public Class GlobalActionFiltersAttribute
    Inherits ActionFilterAttribute

    Public Overrides Sub OnActionExecuting(filterContext As System.Web.Mvc.ActionExecutingContext)
        '   Detects if it is a post request.
        If filterContext.HttpContext.Request.RequestType.ToLower.Equals("post") Then
            '   Adds "no-store", "no-cache" to cache-control header and "no-cache" to pragma header.
            filterContext.HttpContext.Response.Cache.SetNoStore()
            filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache)
        End If
        MyBase.OnActionExecuting(filterContext)
    End Sub

End Class 

The final step is adding following code to Application_Start sub on global.asax.vb file:

VB.NET
GlobalFilters.Filters.Add(New GlobalActionFiltersAttribute())

As result, all response header on action requested as post in our project will look like this:

Cache-Controlno-cache, no-store
Content-Encodinggzip
Content-Length1426
Content-Typetext/html; charset=utf-8
DateMon, 19 Nov 2012 11:23:56 GMT
Expires-1
Pragmano-cache
ServerMicrosoft-IIS/7.0
VaryAccept-Encoding
X-Powered-ByASP.NET

Points of Interest 

It's obvious that even on post requests we must specify that response must not be cached. This fact is oppossed to http specifications, saying that post requests must be revalidated always. I think that Apple skipped this rule in a try to save more battery and 3G traffic than their competitors, but it's a really very bad idea. The good thing is that I've learned to apply global filters to any action in a MVC project.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) TokApp OnLine, S.L.
Spain Spain
Software architect expert in #cloudcomputing #server #mobile #web #dotnet #php #sql #HTML5

Comments and Discussions

 
-- There are no messages in this forum --