微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

OutputCache VaryBy... 如果用户已登录?

如何解决OutputCache VaryBy... 如果用户已登录?

我试图让我的主页加载速度尽可能快,所以我想利用 OutputCache。但我想根据用户是否登录(以及是否处于特定角色)显示不同的主页。

这是我当前在 HomeController 中的代码。我知道我可以使用 varyByParam 并传递一个参数,但是如果用户请求我的域的根目录,这将不起作用。我是否只需要满足于使用 MemoryCache 来缓存模型数据?

我已经尝试按照此处描述的 cookie 来改变缓存 OutputCache VaryByCustom cookie value,但我无法让它工作。

我认为我所需要的只是能够在返回 OutputCache 之前检测用户是否已登录,然后我可以将他们重定向到另一个页面(如果是)?

[OutputCache(Duration = 604800,Location = System.Web.UI.OutputCacheLocation.Server)]//1 week
public ActionResult Index()
{
    if (HttpContext != null && !String.IsNullOrEmpty(HttpContext.User.Identity.Name))
    {
        var user = userService.GetUserByEmail(HttpContext.User.Identity.Name);
        if (user != null && user.IsUserSubscribed)
        {
             //get model data and show a different view if user is subscribed
        }
        else
        {
            //get model data and show different view if user logged in but not subscribed
        }
    }
    else
    {
        //get model data and show default view
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。