3

I have a (working) MVC-application that uses Session properties on multiple parts:

return httpContext.Session[SPContextKey] as SharePointAcsContext;

(ignore that this is sharepoint; This problem isn't be SP-specific)

This works fine until I try to enable Outputcaching:

[OutputCache (Duration =600)]
public ActionResult Select() {
  DoSelect();
}

When the content is cached, httpContext.Session becomes NULL.

Is there a way to keep the Session data and also use caching?

Ole Albers
  • 8,715
  • 10
  • 73
  • 166
  • http://stackoverflow.com/questions/5447611/why-are-there-two-incompatible-session-state-types-in-asp-net might be helpful to you – rashfmnb Mar 22 '16 at 17:47

1 Answers1

2

I found the solution myself. It took a while until I came to the conclusion that - if the data is cached - there shouldn't be any individual code at all that is run. Cause that should be the main purpose of the cache: Don't run any code when the data is cashed.

That led me to the conclusion that the code causing the problem must be run before the cache. And so the "bad boy" was easy to find. Another attribute (in this case an AuthorizeAttribute) that is before the OutputCache-Attribute in the code is still run when caching applies but cannot access the Session:

[Route("{id}")]
[UserAuth(Roles =Directory.GroupUser)]
[JsonException]
[OutputCache(Duration = 600)]
public ActionResult Select()
{
  DoSelect();
}

Putting the UserAuth-Attribute BELOW the OutputCache-Attribute solved the problem

Ole Albers
  • 8,715
  • 10
  • 73
  • 166