C# OutputCache阻止我的用户名显示在标题中

C# OutputCache阻止我的用户名显示在标题中,c#,asp.net-mvc,asp.net-mvc-4,caching,identity,C#,Asp.net Mvc,Asp.net Mvc 4,Caching,Identity,在我的网站上,我在my_Layout.cshtml中定义了一个标题。在该文件中,我正在执行以下操作: <li class="dropdown"> @if (Request.IsAuthenticated) { <a href="#" class="dropdown-toggle menuItem" data-toggle="dropdown" style="color: red;">@User.Identity.Name <b cla

在我的网站上,我在my_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>
我使用了这个库(spender在上面的评论中提到)来解决一个非常类似的问题

一旦您的项目引用了MVCDonutCache库,您就可以调用扩展的
Html.Action
方法将其从缓存中排除,例如

// Action(this HtmlHelper htmlHelper, string actionName, string controllerName, bool excludeFromParentCache)
@Html.Action("LoginStatus", "Home", true) 

要做到这一点,您显然需要将不希望缓存的零件隔离到其自己的操作和局部视图中。

情况可能更糟。。。假设页面在您登录时被缓存。。。敏感信息可能被泄露。我想知道现在mvc是否支持甜甜圈缓存…MvcDonutCaching@spender非常有趣,所以我是否需要同时指定甜甜圈缓存和常规OutputCache属性?或者我应该替换我的OutputCache而改用DonutCache吗?恐怕没办法。上次我需要它时,它不可用@spender抱歉,请重新阅读,上面写着“替换”……今晚我可以继续我的项目时,我会试一试,谢谢。所以我觉得当前使用内置Request.IsAuthenticated的方法在我的标题中不起作用,即使有甜甜圈缓存。我需要重新考虑,我的标题在所有页面上都必须是静态的……我看不出有任何理由它不起作用。在您的_布局文件中,您将使用
HTML.Action
调用一个控制器方法来替换您发布的HTML,该控制器方法呈现包含HTML的部分视图。哦,我想我只是假设我不能将部分视图扔到
  • 元素中……您可以将它扔到任何您喜欢的地方:)
    // Action(this HtmlHelper htmlHelper, string actionName, string controllerName, bool excludeFromParentCache)
    @Html.Action("LoginStatus", "Home", true)