How to disable browser cache in asp.net mvc
.Net
How to disable browser cache in asp.net mvc
Problem:
asp.net mvc some time show old data in screens. to avoid this issue we need to disable cache.
Solution
1) Create a Filter
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;namespace Controllers.Filter
{
public class CacheFilter : ActionFilterAttribute
{
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);
}
}
}
2) Register filter on OnResultExecuting
using System.Web;
using System.Web.Mvc;
using project.Controllers.Filternamespace Projecdt.App_Start
{
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new RefreshSessionController());
filters.Add(new Filter.CacheFilter());
}
}
}
How to disable browser cache in asp.net mvc
Problem:
asp.net mvc some time show old data in screens. to avoid this issue we need to disable cache.
Solution
1) Create a Filter
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;namespace Controllers.Filter
{
public class CacheFilter : ActionFilterAttribute
{
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);
}
}
}
2) Register filter on OnResultExecuting
using System.Web;
using System.Web.Mvc;
using project.Controllers.Filternamespace Projecdt.App_Start
{
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new RefreshSessionController());
filters.Add(new Filter.CacheFilter());
}
}
}
Share This with your friend by choosing any social account
You may also read following recent Post
![]() |
How old are you and what is your net worth? Also, what is your educational background and what sort of a job do you do? Do you enjoy your work?
48 By Nauman Shafi |
![]() |
create appointment booking page with available time slot using database ms sql server
54 By Junaid A |
![]() |
what is Asp.Net Core
66 By Junaid A |
![]() |
publish has encountered an error Object reference not set to an instance of an object A diagnostic log has been written to following location
99 By Junaid A |
![]() |
The provided URI scheme 'http' is invalid; expected 'https'." & vbCrLf & "Parameter name: via
107 By Junaid A |
![]() |
C# Language Basics
1676 By |
![]() |
Crud in Asp.NET using tabs
640 By Haider |
![]() |
Parser Error
704 By Usman Jafar |
![]() |
What are 3 C
198 By |
![]() |
what is .net
421 By |
![]() |
How to post uploaded file and form data in MVC using jquery?ts Common scenario where you want to pos
228 By |
![]() |
MVC url routing
268 By |
![]() |
Learn about Session
290 By |