public class BaseController : Controller
{
/// <summary>
/// 登陆用户的信息
/// </summary>
public Admin LogonAdmin = null;
/// <summary>
/// 控制器加载调用
/// </summary>
/// <param name="e"></param>
protected override void Initialize(RequestContext requestContext)
{
if (Request != null)
{
if (HttpContext.Request.Cookies["user"] != null)
{
//账户 是否启用 名称 密码 创建时间
string[] logonadmininfo = Enc_DecCookies.DecryptLittle(Request.Cookies["admin"].Value, 130).Split('|');
if (logonadmininfo.Length > 0)
{
Admin adm = new Admin();
adm.Account = logonadmininfo[0];
adm.AdminId = Convert.ToInt32(logonadmininfo[1]);
adm.Isenable = Convert.ToBoolean(logonadmininfo[2]);
adm.Name = logonadmininfo[3];
adm.Password = logonadmininfo[4];
adm.Time = Convert.ToDateTime(logonadmininfo[5]);
LogonAdmin = adm;
}
}
}
}
}
public class SAdminController : BaseController
{
protected override void Initialize(RequestContext requestContext)
{
base.Initialize(requestContext);
}
//
// GET: /SAdmin/
堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。]
System.Web.Mvc.Controller.PossiblyLoadTempData() +11
System.Web.Mvc.Controller.ExecuteCore() +38
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
System.Web.Mvc.Async.<>c__DisplayClass8`1.<
报错是哪一行?
但是发现你少了
base.Initialize(requestContext);
至少有两处还需要判断:
1、
Request.Cookies["admin"]是否为null,
Request.Cookies["admin"].Value是否为null
2、
logonadmininfo是否为null
另外:
if (HttpContext.Request.Cookies["user"] != null)
为什么后面又改成Request.Cookies["admin"].Value
代码都贴出来了。把异常堆栈也贴出来吧。那样一看就清楚了。
楼主不会调试吗?
用try