I have a page with outputcache right above the action in a controller class. What I want is to disable this outputcache for myself. Can it be done by IP?
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Make a special filter which will - given some condition - disable the output cache:
public class MyFilterAttribute : ActionFilterAttribute {
public override void OnActionExecuting(ActionExecutingContext filterContext) {
base.OnActionExecuting(filterContext);
filterContext.HttpContext.Response.Cache.AddValidationCallback(MyCacheValidateHandler, null);
}
private static void MyCacheValidateHandler(HttpContext context, object data, ref HttpValidationStatus validationStatus) {
if (context.Request.UserHostAddress == "100.100.100.100") {
// don't serve this page from cache
validationStatus = HttpValidationStatus.IgnoreThisRequest;
// additionally, ignore the [OutputCache] for this particular request
context.Response.Cache.SetNoServerCaching();
context.Response.Cache.SetNoStore();
}
}
}
Note the logic in the MyCacheValidateHandler() method. If the condition holds, then the filter needs to tell the server two things. First, don't serve the current page from cache (IgnoreThisRequest). Second, suppress [OutputCache] for this particular request (SetNoServerCaching / SetNoStore).
Then just apply this filter to your action alongside the regular [OutputCache]:
Is Custom Filter execute before CacheAttribute or it needs to give an order
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
isidat
Member
7 Points
17 Posts
Output Caching For Several Client IP's
Apr 07, 2010 08:06 AM|LINK
Hi forum,
I have a page with outputcache right above the action in a controller class. What I want is to disable this outputcache for myself. Can it be done by IP?
Something like that: [OutputCache(Duration = 60, VaryByParam = "None", IgnoreIP = "100.100.100.100")]
Any help will be appriciated...
imran_ku07
All-Star
42758 Points
7188 Posts
MVP
Re: Output Caching For Several Client IP's
Apr 07, 2010 01:00 PM|LINK
[link=http://codebetter.com/blogs/darrell.norton/archive/2004/05/04/12724.aspx]VaryByCustom[/link]
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
isidat
Member
7 Points
17 Posts
Re: Output Caching For Several Client IP's
Apr 07, 2010 01:21 PM|LINK
Thanks but can you expand your answer a little bit? What should I have to write in VaryByCustom attribute in order to check an IP?
[OutputCache(Duration = 60, VaryByParam = "None", VaryByCustom = "???")]
levib
Star
7636 Points
1092 Posts
AspNetTeam
Re: Output Caching For Several Client IP's
Apr 07, 2010 07:26 PM|LINK
Make a special filter which will - given some condition - disable the output cache:
public class MyFilterAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); filterContext.HttpContext.Response.Cache.AddValidationCallback(MyCacheValidateHandler, null); } private static void MyCacheValidateHandler(HttpContext context, object data, ref HttpValidationStatus validationStatus) { if (context.Request.UserHostAddress == "100.100.100.100") { // don't serve this page from cache validationStatus = HttpValidationStatus.IgnoreThisRequest; // additionally, ignore the [OutputCache] for this particular request context.Response.Cache.SetNoServerCaching(); context.Response.Cache.SetNoStore(); } } }Note the logic in the MyCacheValidateHandler() method. If the condition holds, then the filter needs to tell the server two things. First, don't serve the current page from cache (IgnoreThisRequest). Second, suppress [OutputCache] for this particular request (SetNoServerCaching / SetNoStore).
Then just apply this filter to your action alongside the regular [OutputCache]:
[OutputCache(Duration = 10, VaryByParam = "None")] [MyFilter] public ActionResult YourMethod() { // ... }Hope this helps!
isidat
Member
7 Points
17 Posts
Re: Output Caching For Several Client IP's
Apr 07, 2010 07:54 PM|LINK
Marked as answer
Thank you
imran_ku07
All-Star
42758 Points
7188 Posts
MVP
Re: Output Caching For Several Client IP's
Apr 08, 2010 05:21 AM|LINK
Is Custom Filter execute before CacheAttribute or it needs to give an order
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
levib
Star
7636 Points
1092 Posts
AspNetTeam
Re: Output Caching For Several Client IP's
Apr 08, 2010 04:20 PM|LINK
It doesn't matter.