我创建了一个所有其他控制器都继承的 ApplicationController.cs。我正在尝试使用此控制器从数据库中捕获用户的全名,设置为 ViewBag 并在 layout.cshtml 中捕获。
我还在 Global.asax 中创建了一个捕获用户登录的会话变量。我的问题是 ApplicationController.cs 中的 Session 变量为空。但是,会话变量在所有子控制器中都正确填充。这是我的 ApplicationController.cs 代码:
public abstract class ApplicationController : Controller
{
private SkadPROEntities db = new SkadPROEntities();
public SkadPROEntities SkadProDB
{
get { return db; }
}
public ApplicationController()
{
//string[] currentUserString = User.Identity.Name.Split('\\');
//string myCurrentUser = currentUserString[1];
//string myCurrentUser = Session["currentUser"].ToString();
var empQuery = from employee in db.PRO_LSN_EMPLOYEE_OBJ
//where employee.LSN_Security_Nbr == Session["currentUser"].ToString()
where employee.LSN_Security_Nbr == "bhopkins"
select employee;
string firstName = "";
foreach (var myEmployee in empQuery)
{
firstName = myEmployee.LSN_cf_First_Name;
}
ViewBag.FirstName = firstName;
ViewBag.EmpName = "Bill Test";
}
}
为什么 ApplicationController.cs 中的会话变量为空,但在所有子控制器中填充?