MVC在导航栏中显示登录用户的名称

时间:2013-06-20 13:17:24

标签: asp.net-mvc asp.net-mvc-4 twitter-bootstrap menubar

我在我的应用程序中使用twitter bootstrap包进行应用程序布局。 我主要使用_BootstrapLayout.basic.cshtml作为我的默认布局。

@using System.Web.Optimization
@using BootstrapSupport
@using NavigationRoutes
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>@ViewBag.Title</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link href="@Styles.Url("~/content/css")" rel="stylesheet"/>
        @RenderSection("head", required: false)
        @RenderSection("jtable", required:false)
        @Html.Partial("_html5shiv")
        @* favicons and touch icons go here *@
    </head>
    <body>
        <div class="navbar navbar-inverse navbar-fixed-top">
            <div class="navbar-inner">
                <div class="container">
                    <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </a>
                    <a class="brand" href="#" title="change in _bootstrapLayout.basic.cshtml">Sorama</a>
                    <div class="nav-collapse collapse">
                        <ul class="nav">
                            @Html.Navigation()
                        </ul>
                    </div>
                </div>
            </div>
        </div>        
        <div class="container">
            @Html.Partial("_alerts")
            @Html.Partial("_validationSummary")
            @RenderBody()   
            <hr>
            <footer>
                <p>&copy; Sorama @System.DateTime.Now.ToString("yyyy")</p>
            </footer> 
        </div>
         @Scripts.Render("~/js")
         @RenderSection("Scripts", required: false)
    </body>
</html>

这给了我一个带导航栏的页面 enter image description here

如何向导航栏右上角的登录用户显示欢迎消息?像Welcome ABC这样的东西!还有一些注销选项? 我唯一知道的是我可以从User.Identity.Name获取当前用户的名字,但我不知道如何让它出现在菜单栏上。 我找不到可以帮助我的东西所以我想我可以在这里得到它。

修改:在视图中添加@User.Identity.Name后 我在<ul>代码@html.navigation之后添加了上述代码 这就是我得到的 enter image description here

我在菜单栏上显示欢迎演示(旁边的个人资料,很难看到),但这与我的预期完全不同。 可以在Bootstrap提供的DefaultRouteConfig中完成某些事情吗?

 public class LayoutsRouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.MapNavigationRoute<ModuleController>("Module", c => c.Index());
            routes.MapNavigationRoute<AccountController>("Hardware", c => c.Login());
            routes.MapNavigationRoute<LayoutsController>("Profile", c => c.Starter())
                  .AddChildRoute<LayoutsController>("Change Password", c => c.Marketing())
                  .AddChildRoute<AccountController>("Add User", c => c.Register())
                  .AddChildRoute<LayoutsController>("Logout", c => c.SignIn())
                ;

        }
    }

1 个答案:

答案 0 :(得分:6)

在包含nav的div元素之后,只需添加另一个div元素并将用户名放在那里。

类似的东西:

<div>@User.Identity.Name</div>

ul标记用于Bootstrap导航,因此创建了

<li> <a href="#"> @User.Identity.Name</a></li> 

应该看起来不错,您可以使用操作链接到用户管理而不是#