Controller:

//把登陆用户名存到cookies中
HttpCookie cook = new HttpCookie("cookusername", UserName.ToString());    
Response.Cookies.Add(cook);

View:

复制代码
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"
                           aria-expanded="false">
                            <i class="fa fa-user-o fa-fw"></i>
                            @if (Request.Cookies["cookusername"]!= null)
                            {
                                @Request.Cookies["cookusername"].Value;
                            }                                              
                            <span class="caret"></span>
                        </a>
复制代码

Session实现:

controller:

 

//登陆成功把用户名存入session
 Session["username"] = UserName.ToString();

View:

复制代码
 <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"
                            aria-expanded="false">
                          <i class="fa fa-user-o fa-fw"></i>
                           @if (Session["username"]!= null)
                          {
                              @Session["username"].ToString();
                             }                                              
                             <span class="caret"></span>
                     </a>
复制代码

在web.config设置Session过期时间

<system.web>
<sessionState mode="InProc" timeout="30"></sessionState>  <!--session过期时间设置-->
</system.web>