Kyle
Kyle

Reputation: 17677

OutputCache by Membership?

I'm just reading about OutputCache, and I see how you can apply VaryByParam to change the cache based on parameters sent to the view, but I would like to change the cache based on both the parameters and the currently logged in user (using Asp.Nets default membership). I've been looking around, but I don't seem to be able to find a way to get this working.

Any suggestions on what I should try?

Upvotes: 1

Views: 177

Answers (1)

moribvndvs
moribvndvs

Reputation: 42497

Use VaryByCustom. I implemented something like this:

[OutputCache(VaryByCustom="user")]
public ActionResult SomeAction()
{
    return View();
}

and in Global.asax.cs

public override string GetVaryByCustomString(HttpContext context, string arg)
{
    if (arg == "user")
    {
        return context.Request.User.Identity.IsAuthenticated ? context.Request.User.Identity.Name : string.Empty;
    }

    return base.GetVaryByCustomString(context, arg);
}

Upvotes: 4

Related Questions