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

2024-01-01

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

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

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

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

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

        }
    }

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

就像是:

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

ul标签的样式适合 Bootstrap 导航,因此创建一个

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

MVC在导航栏中显示登录用户的名称 的相关文章

随机推荐