MVC Displaying name of logged in user in nav bar

18,887

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 #

Share:
18,887
Cybercop
Author by

Cybercop

Master student of Web Science. Block chain and Machine Learning enthusiast

Updated on June 28, 2022

Comments

  • Cybercop
    Cybercop almost 2 years

    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 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.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 enter image description here

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