Я пытаюсь сделать что-то, что должно быть очень простым ... У меня есть сайт с раскрывающимся списком, из которого пользователь выбирает группу. После этого пользователь перемещается по сайту, используя аргументы строки запроса из меню. Поэтому я хочу, чтобы кеширование зависело от строки запроса - похоже, это работает. Я также хочу, чтобы кеш зависел от выбранной ими группы.
Но когда строка запроса пуста, кажется, что ни один из элементов кеша не работает - страница соответствует той же версии, которая была для последней выбранной группы. Моя директива кеширования выглядит так:
<%@ OutputCache Duration="300" VaryByCustom="currentAtomId" VaryByParam="documentId;folderId;sectionId;renderMode;typeId" %>
Мой код DifferentByCustom выглядит так:
public override string GetVaryByCustomString(HttpContext context, string custom)
{
switch (custom)
{
case "currentAtomId":
var currentAtomId = SecurityManifold.Create().CurrentAtomId;
var returnString = currentAtomId == null ? Guid.NewGuid().ToString() : currentAtomId.ToString();
return returnString;
default:
throw new ArgumentException(string.Format("Argument '{0}' is not a valid cache argument.", custom));
}
}
Вызов CurrentAtomId сводится к следующему:
public static int? GetCurrentAtomIdFromContext(HttpContext context)
{
int entityId;
if (context.Session == null)
{
throw new InvalidOperationException("Session is null");
}
var sessionEntityId = context.Session["CurrentEntityId"];
if (sessionEntityId == null || string.IsNullOrEmpty(sessionEntityId.ToString()))
{
return null;
}
if (!int.TryParse(sessionEntityId.ToString(), out entityId))
{
return null;
}
return entityId;
}
Наконец,код, который определяет CurrentEntityId, следующий:
var selectedEntityId = this.lstSecurityEntities.SelectedValue;
if (string.IsNullOrEmpty(selectedEntityId))
{
return;
}
Session["CurrentEntityId"] = selectedEntityId;
var possibleQueryString = Request.QueryString.ToString();
if (!string.IsNullOrEmpty(possibleQueryString))
{
possibleQueryString = "?" + possibleQueryString;
}
Response.Redirect("default.aspx" + possibleQueryString);
Я сбит с толку. Приветствуются любые мысли.