1 回答
TA貢獻1796條經驗 獲得超4個贊
理想情況下,您應該將其加載到您的模型中:
public ActionResult Index()
{
FulfillmentContext db = new FulfillmentContext();
string username =
HttpContext.Current.User.Identity.GetUserName();
var currentUser = (from u in db.AspNetUsers
join ur in db.AspNetUserRoles on u.Id equals ur.UserId
join r in db.AspNetRoles on ur.RoleId equals r.Id
where u.UserName == username
select u).First();
var model = new Model();
model.CurrentUser = currentUser;
return View(model);
}
但是,您可以創建一個 HTML 幫助程序擴展:
public static AspNetUser GetCurrentUser(this HtmlHelper helper)
{
FulfillmentContext db = new FulfillmentContext();
string username = HttpContext.Current.User.Identity.GetUserName();
var currentUser = (from u in db.AspNetUsers
join ur in db.AspNetUserRoles on u.Id equals ur.UserId
join r in db.AspNetRoles on ur.RoleId equals r.Id
where u.UserName == username
select u).First();
return currentUser;
}
然后從您的視圖中調用它:
@{
var user = Html.GetCurrentUser();
}
- 1 回答
- 0 關注
- 112 瀏覽
添加回答
舉報
