我session["username]"在Login.cs用户想要登录时定义
public static void SetLogin(string username, bool remmember)
{
HttpContext.Current.Session["username"] = username;
}
Run Code Online (Sandbox Code Playgroud)
我想session["username"]在UserPanelController中使用值
public class UserPanelController : Controller
{
Customer cu;
Customer.customer cust;
public UserPanelController()
{
if (Login.ChekLogin())
{
cust = cu.Read(int.Parse(Session["username"].ToString()));
// error Occur here Session is null
}
}
}
Run Code Online (Sandbox Code Playgroud)
我无法访问会话,因为它是null
您似乎试图在ASP.NET MVC控制器的构造函数中使用Session.它是空的是正常的.会话在稍后阶段初始化 - 在Initialize方法中:
public class UserPanelController : Controller
{
Customer cu;
Customer.customer cust;
protected override void Initialize(RequestContext requestContext)
{
base.Initialize(requestContext);
if (Login.ChekLogin())
{
cust = cu.Read(int.Parse(Session["username"].ToString()));
// error Occur here Session is null
}
}
}
Run Code Online (Sandbox Code Playgroud)
此外,您似乎正在尝试将用户名转换为整数.你确定是这样的吗?
另外,为什么你使用Session来为FormsAuthentication已经为你处理过的东西?
| 归档时间: |
|
| 查看次数: |
5212 次 |
| 最近记录: |