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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17214701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 17:23:27  来源:igfitidea点击:

MVC Displaying name of logged in user in nav bar

asp.net-mvcasp.net-mvc-4twitter-bootstrapmenubar

提问by Cybercop

I'm using twitter bootstrap package in my application for my app layout. I mostly use _BootstrapLayout.basic.cshtml as my default layout.

我在我的应用程序布局中使用 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>

this gives me a page with nav bar enter image description here

这给了我一个带有导航栏的页面 在此处输入图片说明

How can i show the Welcome message to the logged in User on the right corner of the nav bar? Something like Welcome ABC! and some log off option on side? The only thing I know is i can get name of current user from User.Identity.Namebut I don't know how to make it appear on menu bar. I couldn't find something that could help me so I thought may be I could get it here.

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

Edit: After @User.Identity.Namebeing added in view I added above mentioned code after <ul>tag with @html.navigationand this is what I get enter image description here

编辑@User.Identity.Name添加到视图中后,我在<ul>标记后添加了上面提到的代码@html.navigation,这就是我得到的 在此处输入图片说明

I get the Welcome Demo on menu bar(next to profile, difficult to see) but it is nothing like I expected. Could something be done in DefaultRouteConfigprovided by Bootstrap?

我在菜单栏上看到了欢迎演示(在个人资料旁边,很难看到),但这与我预期的完全不同。DefaultRouteConfigBootstrap 提供的可以做些什么吗?

 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())
                ;

        }
    }

回答by Richard Seal

After the div element that contains the nav, just add another div element and put the username in there.

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

Something like:

就像是:

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

ultag is styled for Bootstrap navigation so creating a

ul标签是为 Bootstrap 导航设计的,因此创建一个

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

should look ok and you can use action link to user management instead of #

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