我正在使用MVC 5/WebApi 2和AngularJs。我想在我的视图中显示登录的用户名。我知道如何使用剃须刀显示这些信息,但我怎样才能用Angular做到这一点?所以基本上我需要用Angular来做到这一点。如何使用AngularJs显示MVC登录用户名
<span >Logged In As: @Html.ActionLink(User.Identity.GetUserName(), "Manage", "Account", routeValues: null, htmlAttributes: new { title = "Manage", @style = "color:white;float:right" })</span>
apiUserController
public class apiUserController : ApiController
{
// GET api/<controller>
public List<ApplicationUser> Get()
{
using (var context = new ApplicationDbContext())
{
List<ApplicationUser> users = new List<ApplicationUser>();
users = context.ApplicationUsers
.ToList();
return users;
}
}
}
更新
public IHttpActionResult Get()
{
using (var context = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
{
var user = context.FindById(User.Identity.GetUserId());
var loggedInUser = user.UserName;
return Ok(loggedInUser);
}
}
所以当你说端点你指的是API控制器?它需要达到应用程序用户属性?查看更新后的帖子请 – texas697 2014-10-07 15:28:39
是的,我指的是api控制器,我看了你的例子,但你应该返回单个登录用户,而不是一个列表。 – 2014-10-07 15:30:15
k,x.UserName导致一些错误。我应该尝试什么?谢谢 – texas697 2014-10-07 15:52:43