0

I have a controller action which receives a Person type parameter. The parameter is bound from a JSON request using the JSON based model binding feature.

My VaryByParam is not working in this case, because the request doesn't hold any "classic" variables (eg. GET or POST), but the data is in the HTTP body in this case. On client-side I use KnockoutJS so I post the data using ko.toJSON method.

How could I achieve output caching based on a field's value in the JSON request?

The example is only for demonstration.

// model

public class Person {
  public int PersonID { get; set; }
  public string Name { get; set; }
}

// action

[OutputCaching(Duration = 60, VaryByParam = "PersonID")]
public JsonResult Process(Person person) {
  ...
}

// client-side

$.post({
  url: '/mycontroller/myaction',
  ...
  data: ko.toJSON(personViewModel),
  ...
});
Zoltán Tamási
  • 12,249
  • 8
  • 65
  • 93

1 Answers1

0

VaryByCustom is your friend!

Override the VaryByCustom method in your global.asax.cs file. This allows you to create your own check and since you have access to the HTTPContext here you will be able to check the post values from the message body.

Check out this blog which explains how you can create your own implementation.

Hope this helps

heymega
  • 9,215
  • 8
  • 42
  • 61
  • thank you, I'm aware of VaryByCustom, I've used it to specify user-level caching. However, to implementing this there seems to be quite complicated, because I should "blindly" parse the JSON request body somehow to extract the required properties, I don't exactly see where to start yet, but I'll give it a try. – Zoltán Tamási Apr 24 '15 at 08:48