我正在设计一个ASP.net MVC4应用程序,我需要获取当前的用户ID,以便将它作为组的所有者ID保存在我的数据库的表中。获取userId的登录用户返回null
这是我使用的代码:
public ActionResult CreateGroup(string groupName, string description)
{
if (Request.IsAuthenticated)
{
Group group = new Group();
Random random = new Random();
int groupId = random.Next(1, 2147483647);
using (CodeShareEntities conn = new CodeShareEntities())
{
try
{
int ownerId = (int) Membership.GetUser(System.Web.HttpContext.Current.User.Identity.Name).ProviderUserKey;
group.GroupId = groupId;
group.GroupName = groupName;
group.Description = description;
group.OwnerId = ownerId;
conn.Group.Add(group);
conn.SaveChanges();
}
catch (Exception e)
{
ModelState.AddModelError("", e.Message);
}
}
return View("ThisGroup");
}
else
{
return View("../Shared/NotLoggedIn");
}
我得到一个NullReferenceException在这条线:
虽然用户名是否正确返回。可能是什么问题?我对web应用程序仍然很陌生,我真的无法弄清楚这一点!
感谢
什么是Guid?以及如何将其转换为整数,以便我可以将其保存在数据库中?感谢您的回答 – Bernice 2013-05-02 19:04:13
尝试与Guid,但仍然没有工作..同样的例外:/ – Bernice 2013-05-02 19:10:08