0

I have this example of OutputCache. My problem is that I want the page to be cached only if the [id] equals to NULL. In all other cases I don't want to have cache at all.

MyController:

[OutputCache(Duration = int.MaxValue, VaryByParam = "id")] public ActionResult Details(int id) {} 

RouteConfig:

routes.MapRoute( name: "edit", url: "edit/{id}", defaults: new { controller = "asd", action = "Details", id = UrlParameter.Optional } ); 
0

1 Answer 1

1

You can specify (and implement) the VaryByCustom parameter of OutputCacheAttribute:

MyController.cs

[OutputCache(Duration = int.MaxValue, VaryByCustom = "idIsNull")] public ActionResult Details(int id) { } 

Global.asax.cs

public override string GetVaryByCustomString(HttpContext context, string arg) { if (arg.ToLower() == "idisnull") { return string.IsNullOrWhiteSpace(Request.QueryString["id"]) ? string.Empty // unique key means it won't have a consistent value to use // as a cache lookup : ((new DateTime(1970, 1, 1) - DateTime.Now).TotalMilliseconds).ToString(); } } 
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, but the [id] is not a query string. See edit for routing details
@user1615362: I don't see it defined in the route url, so what's the problem?
This IsNullOrWhiteSpace(Request.QueryString["id"]) will always be true.
There are no QueryStiring params at all, I am using restful URLs and the [id] is a url segment.
@user1615362: once again....not according to your route. I don't see {id} anywhere in that route, therefore it's either a POST value (in which case Request.Form should be used) or a GET variable (stick with Request.QueryString).
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.