【发布时间】:2014-06-17 21:58:51
【问题描述】:
在我的网站上,我的 _Layout.cshtml 中定义了一个标题。在那个文件中,我正在这样做:
<li class="dropdown">
@if (Request.IsAuthenticated)
{
<a href="#" class="dropdown-toggle menuItem" data-toggle="dropdown" style="color: red;">@User.Identity.Name <b class="caret"></b></a>
}
else
{
<a href="#" class="dropdown-toggle menuItem" data-toggle="dropdown">Profile <b class="caret"></b></a>
}
<ul class="dropdown-menu">
@if (!Request.IsAuthenticated)
{
<li><a href="/Account/Register">Register</a></li>
<li><a href="/Account/Login">Login</a></li>
<li><a href="/Account/ForgotPassword">Forgot Password</a></li>
}
else
{
<li><a href="/Account/ChangePassword">Change Password</a></li>
<li><a href="/Account/EditProfile">Edit Profile</a></li>
<li><a href="/Account/Logout">Logout</a></li>
}
</ul>
</li>
所以,我想动态显示我的菜单项名称,以及基于用户是否登录的内容。
我的所有控制器中 99% 的方法都实现了 [OutputCache] 属性。因此,在我登录该站点后,菜单项仍然显示“个人资料”以及与个人资料一起出现的相应菜单项(即注册、忘记密码等)。
我是否必须关闭网站中的缓存才能在登录后立即显示用户名?这在我的开发环境中非常有效,因为我在缓存属性周围使用了#IF DEBUG 语句...
例如,这是我的 HomeController:
#if !DEBUG
[OutputCache(Duration = 86400)]
#endif
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
【问题讨论】:
-
可能会更糟...假设您登录时页面被缓存...敏感信息可能会泄露。我想知道 mvc 现在是否支持甜甜圈缓存...
-
MvcDonutCaching github.com/moonpyk/mvcdonutcaching
-
@spender 非常有趣,所以我是否需要同时指定 DonutCache 以及常规的 OutputCache 属性?或者我应该替换我的 OutputCache 并改用 DonutCache?
-
恐怕没办法。上次我需要它时它不可用!
-
@spender 抱歉,请重新阅读它,上面说要替换....今晚我可以在我的项目上进行更多工作时,我会试一试,谢谢。
标签: c# asp.net-mvc asp.net-mvc-4 caching identity