MVC Displays the registered user name in the navigation bar.

I use twitter bootstrap package in my application for my application layout. I mainly use _BootstrapLayout.basic.cshtml as the 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 navigation bar page enter image description here

How can I show a welcome message to a registered user in the right corner of the navigation bar? Something like Welcome ABC! and some side logout options? The only thing I know is to get the name of the current user from User.Identity.Name , but I do not know how to do this in the menu bar. I could not find something that could help me, so I thought that I could get it here.

Edit : after adding @User.Identity.Name I added the above code after the <ul> tag using @html.navigation and this is what I get enter image description here

I get a welcome demo in the menu bar (next to a profile that is hard to see), but I didn’t expect anything like that. Can something do in the 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()) ; } } 
+4
source share
1 answer

After the div element containing the navigation, just add another div element and put the username here.

Sort of:

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

ul is for bootstrap navigation, so creating

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

should look fine, and you can use the action link to manage users instead of #

+6
source

Source: https://habr.com/ru/post/1487296/


All Articles