Back button issue in MVC 6 after login and logout

☆樱花仙子☆ 提交于 2019-12-05 18:30:02

To disable caching you can apply the following to each controller you wish to disable caching on

[ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)]

You can also setup named cache profiles and configure the settings at runtime.

Cache can be disabled by applying the [ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)] on each controller for which you wish to disable caching.

This can be applied to all controllers globally by adding your custom filter in startup.cs while configuring MVC services with ASP.Net Core 1.0.-* and MVC6.

First create a custom cache filter implementing the ResponseCacheAttribute as follows

public class ResponseCacheFilter : ResponseCacheAttribute
{
    public ResponseCacheFilter() : base()
    {
        Location = ResponseCacheLocation.None;
        NoStore = true;
    }
}

This should be added in startup.cs file as follows.

public void ConfigureServices(IServiceCollection services)
{
   // Add framework and custom services.
   services.AddMvc(config =>
     {
         config.Filters.Add(new ResponseCacheFilter());
     });
}

See the configuration of ResponseCache at https://docs.asp.net/en/latest/performance/caching/response.html#responsecache-attribute

Just Add This Code in Global.asax.cs

protected void Application_BeginRequest()
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
        Response.Cache.SetNoStore();
    }

Just add these lines to your Global.asax.cs file.

protected void Application_BeginRequest()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!