Response Caching using VaryByQueryKeys with dictionary querystring argument

1.6k Views Asked by At

My ASP.NET Core MVC application has a controller method with a dictionary argument, passed via the request query string:

public async Task<IActionResult> Get([FromQuery] Dictionary<string, string> filterCriteria)
{
}

The application uses Response Caching with the VaryByQueryKeys option to return cached responses for requests that have previously been served. This works as required for simple querystring values, using an attribute on the method with the following syntax: [ResponseCache(VaryByQueryKeys = new[] { "argName" }, Duration = 20)]

However, I want to use it with a dictionary argument illustrated above.

Can it be used with an object argument like a dictionary? What is the syntax to use when specifying the list of querystring keys in the ResponseCache attribute?

1

There are 1 best solutions below

1
On

It turns out you can specify queryKeys for a dictionary argument using the following syntax:

[ResponseCache(VaryByQueryKeys = new[] { "argName[key1]", "argName[key2]", ... }, Duration = 20)]