从的OutputCache在标题显示停止我的用户名 [英] OutputCache stopping my username from displaying in header
问题描述
在我的网站,我有我的_Layout.cshtml定义的标题。在该文件中,我这样做是:
<李班=下拉菜单>
@if(Request.IsAuthenticated)
{
< A HREF =#类=下拉菜单,切换菜单项数据切换=下拉菜单的风格=颜色:红色;> @ User.Identity.Name< B类=插入符号>&LT ; / b>< / A>
}
其他
{
< A HREF =#类=下拉菜单,切换菜单项数据切换=下拉菜单>简介< B类=插入符号>< / B>< / A>
} < UL类=下拉菜单>
@if(!Request.IsAuthenticated)
{
<立GT;< A HREF =/帐号/注册>注册< / A>< /李>
<立GT;< A HREF =/帐号/登录>登录和LT; / A>< /李>
<立GT;< A HREF =/帐号/ ForgotPassword>忘记密码< / A>< /李>
}
其他
{
<立GT;< A HREF =/帐号/的ChangePassword>修改密码< / A>< /李>
<立GT;< A HREF =/帐号/ EditProfile>编辑个人资料< / A>< /李>
<立GT;< A HREF =/帐号/注销>注销< / A>< /李>
}
< / UL>
< /李>
所以,我想动态显示我的菜单项名称,以及根据用户是否登录与否的内容
在我所有的控制器的方法,99%实现了[的OutputCache]属性。正因为如此,当我登录网站,菜单项仍表示档案有与档案(又名注册,忘记密码等),走相应的菜单项。
我一定要关闭缓存在我的网站上以供用户名立即显示在登录后?这在我的开发环境完美的,因为我用DEBUG #IF语句在我的缓存属性...
例如,这里是我的HomeController:
#如果!DEBUG
[的OutputCache(持续时间= 86400)
#万一
公共类HomeController的:控制器
{
公众的ActionResult指数()
{
返回查看();
}
}
我已经使用了甜甜圈缓存库(通过花钱在上面的评论中提到)来解决一个非常类似的问题。
在您的项目有向MvcDonutCaching库的引用,可以调用扩展 Html.Action
方法来从缓存例如排除。
//行动(这的HtmlHelper的HtmlHelper,串actionName,串controllerName,布尔excludeFromParentCache)
@ Html.Action(LoginStatus,家,真)
要做到这一点,你显然需要你不想缓存自身的行动和局部视图部分隔离开来。
On my website I have a header defined in my _Layout.cshtml. In that file, I'm doing this:
<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>
So, I'm wanting to dynamically display my menu item name, as well as the contents based on whether the user is logged in or not.
99% of the methods in all of my controllers implement the [OutputCache] attribute. Because of this, after I login to the site, the menu item still says "Profile" with the corresponding menu items that go along with Profile (aka Register, Forgot Password, etc).
Do I have to turn off caching in my site in order for the username to show up immediately after logging in? This works perfect in my Development environment because I use #IF DEBUG statements around my caching attributes...
For example, here's my HomeController:
#if !DEBUG
[OutputCache(Duration = 86400)]
#endif
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
I have used the donut caching library (mentioned by spender in the comments above) to solve a very similar problem.
Once your project has a reference to the MvcDonutCaching library, you can call an extended Html.Action
method to exclude it from the cache e.g.
// Action(this HtmlHelper htmlHelper, string actionName, string controllerName, bool excludeFromParentCache)
@Html.Action("LoginStatus", "Home", true)
To do this, you obviously would need to isolate the part you don't want to cache to its own action and partial view.
这篇关于从的OutputCache在标题显示停止我的用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!