MVC Role users error instance
-
I created a few roles for users and I'm trying to assing a role to a specific user but I get this error:
An exception of type 'System.NullReferenceException' occurred in Microsoft.Owin.Host.SystemWeb.dll but was not handled in user code
Additional information: Object reference not set to an instance of an object.
If there is a handler for this exception, the program may be safely continued.
and
Line 47: get
Line 48: {
Line 49: return _userManager ?? HttpContext.GetOwinContext().GetUserManager();
Line 50: }
Line 51: private setIn AccountController I don't have anything for this option. This is the function from Controller that should assing the role:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult RoleAddToUser(string UserName, string RoleName)
{
ApplicationUser user = context.Users.Where(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
var account = new AccountController();
account.UserManager.AddToRole(user.Id, "RoleName");ViewBag.ResultMessage = "Role created successfully !"; // prepopulat roles for the view dropdown var list = context.Roles.OrderBy(r => r.Name).ToList().Select(rr => new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList(); ViewBag.Roles = list; return View("ManageUserRoles"); }
Where is the mistake?
-
I created a few roles for users and I'm trying to assing a role to a specific user but I get this error:
An exception of type 'System.NullReferenceException' occurred in Microsoft.Owin.Host.SystemWeb.dll but was not handled in user code
Additional information: Object reference not set to an instance of an object.
If there is a handler for this exception, the program may be safely continued.
and
Line 47: get
Line 48: {
Line 49: return _userManager ?? HttpContext.GetOwinContext().GetUserManager();
Line 50: }
Line 51: private setIn AccountController I don't have anything for this option. This is the function from Controller that should assing the role:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult RoleAddToUser(string UserName, string RoleName)
{
ApplicationUser user = context.Users.Where(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
var account = new AccountController();
account.UserManager.AddToRole(user.Id, "RoleName");ViewBag.ResultMessage = "Role created successfully !"; // prepopulat roles for the view dropdown var list = context.Roles.OrderBy(r => r.Name).ToList().Select(rr => new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList(); ViewBag.Roles = list; return View("ManageUserRoles"); }
Where is the mistake?
Did you DEBUG your code? Where did the exception occur?
_userManager ?? HttpContext.GetOwinContext().GetUserManager();
What is _userManager AND HttpContext.GetOwinContext().GetUserManager() are NULL. What would then be the behaviour?
In Word you can only store 2 bytes. That is why I use Writer.