某些用户的MVC应用程序SESSION为null

时间:2014-11-03 17:17:16

标签: asp.net-mvc session null

我的问题在于我为学校开发的系统。我们遇到了一些用户的问题。这是错误:

[NullReferenceException: Object reference not set to an instance of an object.]
_360Feedback.Controllers.SurveyController.Answers() +170
lambda_method(Closure , ControllerBase , Object[] ) +78
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +273
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +38
System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +119
System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +452
System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +15
System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +33
System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +240
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +28
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +42
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288

由于某种原因,会话对象不保留学生ID。我无法重新生成错误,所以我很难找到造成这种情况的原因。

将会话设置为cookieless似乎会减少用户的错误,但仍会为某些用户生成错误。

我可以看到,大约90%的用户一切正常。我将发布使用此会话ID的控制器代码。我希望你们能帮助我。

public ActionResult Start(string x)
{
    string decryptedstudentIDString = EncryptionHelper.Decrypt(x);
    int studentID = Convert.ToInt32(decryptedstudentIDString);
    Student student = repo.FindStudentWithID(studentID);
    Session["StudentName"] = student.Name;
    Session["StudentID"] = student.StudentID;
    if (student.CompletedAnswers == true)
    {
        return RedirectToAction("Completed");
    }
    return View(student);
}
public ActionResult Answers()
{
    Student student = repo.FindStudentWithID(Convert.ToInt32(Session["StudentID"]));
    if (student.CompletedAnswers == true)
    {
        return RedirectToAction("Completed");
    }

    List<Student> remainingStudentInGroup = repo.GetRemainingStudentIDsInGroup(student.StudentID);
    student.Answers.Add(new AnswerSet() { AssociatedStudentID = student.StudentID, AssociatedStudentName = student.Name });
    foreach (var studentInGroup in remainingStudentInGroup)
    {
        student.Answers.Add(new AnswerSet() { AssociatedStudentID = studentInGroup.StudentID, AssociatedStudentName = repo.FindStudentWithID(studentInGroup.StudentID).Name });
    }
    Group group = repo.GetGroupAssociatedWithStudent(student.StudentID);
    Session["StudentID"] = student.StudentID;
    Session["GroupSize"] = group.Students.Count;
    return View(student);
}
[HttpPost]
public ActionResult Answers(Student student)
{
    int s = repo.FindStudentWithID(Convert.ToInt32(Session["StudentID"])).StudentID;
    foreach (var answerSet in student.Answers)
    {
        repo.AddAnswerSetToStudent(answerSet, s);
    }
    return RedirectToAction("Completed");
}
public ActionResult Completed()
{
    Student student = repo.FindStudentWithID(Convert.ToInt32(Session["StudentID"]));
    if (student.CompletedAnswers == false)
    {
        repo.SetBooleanForCompletedAnswerToTrue(student.StudentID);
        Group group = repo.GetGroupAssociatedWithStudent(student.StudentID);
        ExcelHelper.FillExcelWithAnswersForStudent(group.ExcelFilePath, student);
        ExcelHelper.CheckIfAllStudentAnswered(group);
    }
    return View();
}

当用户提交表单时发生错误,当他们发布答案时,StudentID为空。

0 个答案:

没有答案