1

I have two pages like (Online page and Offline page) using SPA in angular and MVC. If a network is lost offline page should work and an Online page should not load, it should display error. (There is no Internet connection). What I did, I have created a Manifest file and added to Layout HTML. whenever I am loading my SPA manifest is added to application cache. Now if a network is gone, when I will click on the Online page nothing is happening due to application cache. What I have to do now I have to remove the application cache from the browser. Please let me know to achieve this scenario.

I tried to remove the application cache using MVC controller, but this one is not working.

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();
Shafi
  • 9
  • 3

2 Answers2

0

use [NoCache] attribute in the controller or action you want to remove the cache.

[Authorize]
[NoCache]

public class LeaveController : Controller
{
0

This should work for you in .NET Core 2.0:

[Authorize]
[ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)]
public class LeaveController : Controller
{
...

This should work for .NET 4.7:

[AuthorizeNoCache]
public class LeaveController : Controller
{

...

In order to use that [AuthorizeNoCaching], you need this:

class AuthorizeNoCaching : AuthorizeAttribute
{
    ////https://stackoverflow.com/questions/10011780/prevent-caching-in-asp-net-mvc-for-specific-actions-using-an-attribute?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        //filterContext.HttpContext.Response.Cache.SetAllowResponseInBrowserHistory(false);  //We want them to be able to see the pages they've been to in the browser history for now.
        //https://stackoverflow.com/questions/866822/why-both-no-cache-and-no-store-should-be-used-in-http-response
        filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        filterContext.HttpContext.Response.Cache.SetNoStore();
        filterContext.HttpContext.Response.Cache.SetExpires(DateTime.Now);
        filterContext.HttpContext.Response.Cache.SetValidUntilExpires(true);
        base.OnAuthorization(filterContext);
    }
}
JakeJ
  • 2,361
  • 5
  • 23
  • 35