问题描述
我试图让我的主页加载速度尽可能快,所以我想利用 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 (将#修改为@)