【发布时间】:2016-05-26 21:24:40
【问题描述】:
当我尝试访问 cookie 时,出现以下异常:
对象引用未设置为对象的实例。
这是有问题的行:
if (Request.Cookies["selectBoxValue"].Value != null)
控制器
[Authorize]
public ActionResult Index()
{
if (Request.Cookies["selectBoxValue"].Value != null)
{
HttpCookie groupId = new HttpCookie("selectBoxValue");
groupId = Request.Cookies["selectBoxValue"];
// Collect all comments connected to current group
int t = Convert.ToInt32(groupId.Value);
pv.ListofComments = db.Comments.Where(dp => dp.GroupID == t).ToList();
// Show only groups connected to current user
var CurrentUser = User.Identity.GetUserId();
var groupUser = db.GroupUsers.Where(u => u.ApplicationUserId == CurrentUser).Select(gr => gr.GroupId).ToList();
pv.GroupList = db.Groups.Where(g => groupUser.Contains(g.Id));
return View(pv);
}
【问题讨论】:
-
@GeorgeStocker 错误发生在实际的 if 语句中:if (Request.Cookies["selectBoxValue"].Value != null)。似乎没有要评估的 cookie 值。没有设置cookie值。这有帮助吗?
-
我不确定这是否是问题所在,但是现在我知道这是问题所在,您有什么解决方法吗?是否可以将 selectBoxValue 设置为默认值?这样它可能是一个对象的实例?
标签: c# asp.net entity-framework cookies