views:

98

answers:

3

Hi,

I'm writing an app and have come across caching problem which I cannot work out.

I have a default Home controller for the site which checks whether the user is authenticated or not. If is not then LogOn View is displayed otherwise the client is redirected to another page.

Along with the LogOn view, also a Check cookie is being sent to user to check on response if his browser supports cookies or not.

When I delete the cookie before sending the form with credentials, Home (Post method) in Home controller displays message that cookies must be enabled with a button which should refresh the page with Logon boxes(http://localhost:1234/)

This button is linked to js refresh function:

var sURL = unescape("/");

function refresh()
{
    window.location.href = sURL;
}

I have implemented CacheFilter which is set on the base controller.

public class NoCache : ActionFilterAttribute, IActionFilter
{
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
        filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
        filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
        filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        filterContext.HttpContext.Response.Cache.SetNoStore();
        base.OnResultExecuting(filterContext);
    }

    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        base.OnResultExecuted(filterContext);
    }
}

and the problem is that if I Immediately click the button to refresh LogOn page, browser read it using its cache. But when I do it after a few seconds then the query is sent to the server. This is wrong, because LogOn page should also create again a Check cookie.

It seems to me that the cache policy is set to 1-2 seconds, and after this time pages are reloaded from the server.

What is wrong? Thanks for help.

A: 

Am I right in saying you're testing this by running this in Visual Studio?

As far as I know you can't enable things like caching in the Visual Studio Development Server. This is something you will enable on your webserver, which I guess will be IIS in your case.

Jaco Pretorius
Yes, I am testing this by running app in Visual Studio.All i wanted to do is to disable permanently caching so that the webbrowsers would load the page everytime from the server, never from cache.
XXL
come back with a test from at least 3 browsers, coz this could be browser related.
mare
A: 

I have seen some similar behaviour, related to the javascript in IE7. The browser 'helpfully' realizes that you've recently requested that URL and therefore serves up the cached version. If this is the case, the solution is to make sure the URL is unique every time by adding a pseudo random number. I use something like:

function refresh()
{
    window.location.href = sURL + "?rnd="+ Math.random();
}
Clicktricity
A: 

ehh what can I say .. Your solution Clicktricity works great ...

But does anyone know how the same can be achieved without such tricks ... against JS

XXL