MVC Displaying name of logged in user in nav bar

人走茶凉 提交于 2020-01-14 14:15:11

问题


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

@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

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.Name but 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.

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

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 DefaultRouteConfig provided by 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())
                ;

        }
    }

回答1:


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

Something like:

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

ul tag is styled for Bootstrap navigation so creating a

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

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



来源:https://stackoverflow.com/questions/17214701/mvc-displaying-name-of-logged-in-user-in-nav-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!