MVC在导航栏中显示已登录用户的名称 [英] MVC Displaying name of logged in user in nav bar
问题描述
我在我的应用程序中使用twitter bootstrap包进行应用程序布局. 我主要使用_BootstrapLayout.basic.cshtml作为默认布局.
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>© 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
如何在导航栏的右上角向已登录的用户显示欢迎"消息?诸如Welcome ABC之类的东西!和一些注销选项的一面?
我唯一知道的是我可以从User.Identity.Name
获取当前用户的名称,但是我不知道如何使它出现在菜单栏上.
我找不到可以帮助我的东西,所以我想也许可以在这里买到它.
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.
编辑:在视图中添加@User.Identity.Name
后
我在<ul>
标记后用@html.navigation
添加了上述代码
这就是我得到的
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
我在菜单栏上看到了欢迎演示(简介旁边,很难看到),但是却没有我期望的那样.
可以在Bootstrap提供的DefaultRouteConfig
中进行某些操作吗?
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())
;
}
}
推荐答案
在包含导航的div元素之后,只需添加另一个div元素,然后在其中放置用户名即可.
After the div element that contains the nav, just add another div element and put the username in there.
类似的东西:
<div>@User.Identity.Name</div>
ul
标签的样式适用于Bootstrap导航,因此可以创建
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 #
这篇关于MVC在导航栏中显示已登录用户的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!