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.
我有一个站点的默认Home控制器,用于检查用户是否已通过身份验证。如果不是,则显示LogOn View,否则客户端将重定向到另一个页面。
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.
与LogOn视图一起,如果他的浏览器支持cookie,还会向用户发送Check cookie以检查响应。
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/)
当我在发送带有凭据的表单之前删除cookie时,Home Controller中的Home(Post方法)显示消息,必须使用按钮启用cookie,该按钮应使用登录框刷新页面(http:// localhost:1234 /)
This button is linked to js refresh function:
此按钮链接到js刷新功能:
var sURL = unescape("/");
function refresh()
{
window.location.href = sURL;
}
I have implemented CacheFilter which is set on the base controller.
我已经实现了在基本控制器上设置的CacheFilter。
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.
问题是,如果我立即单击按钮刷新LogOn页面,浏览器将使用其缓存读取它。但是当我在几秒钟后执行此操作时,查询将被发送到服务器。这是错误的,因为LogOn页面还应该再次创建一个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.
在我看来,缓存策略设置为1-2秒,在此之后页面从服务器重新加载。
What is wrong? Thanks for help.
哪里不对?感谢帮助。
2 个解决方案
#1
1
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:
我看到了一些类似的行为,与IE7中的javascript有关。浏览器“帮助”意识到您最近请求了该URL,因此提供了缓存版本。如果是这种情况,解决方案是通过添加伪随机数确保URL每次都是唯一的。我使用类似的东西:
function refresh()
{
window.location.href = sURL + "?rnd="+ Math.random();
}
#2
0
Am I right in saying you're testing this by running this in Visual Studio?
我是否正确地说你在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.
据我所知,您无法在Visual Studio开发服务器中启用缓存等功能。这是您将在您的网络服务器上启用的内容,我想这将是您的IIS。
#1
1
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:
我看到了一些类似的行为,与IE7中的javascript有关。浏览器“帮助”意识到您最近请求了该URL,因此提供了缓存版本。如果是这种情况,解决方案是通过添加伪随机数确保URL每次都是唯一的。我使用类似的东西:
function refresh()
{
window.location.href = sURL + "?rnd="+ Math.random();
}
#2
0
Am I right in saying you're testing this by running this in Visual Studio?
我是否正确地说你在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.
据我所知,您无法在Visual Studio开发服务器中启用缓存等功能。这是您将在您的网络服务器上启用的内容,我想这将是您的IIS。