我在将模型对象传递给存储库方法时遇到错误(_accountObj )
我尝试在控制器中的接口的帮助下调用存储库方法,因为我正在使用依赖注入,但此时_accountObj.SaveAdmin(_accountModel)
。我收到这样的错误对象引用未设置到对象的实例 - “_accountObj 为空”。我无法将用户注册数据保存到数据库的存储库中。请帮助我解决我的问题。我还将上传代码和错误的图像,以便您更好地理解。
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Security;
using Inspinia_MVC5.Models;
using Domain.Models;
using Services.Interface;
namespace Inspinia_MVC5.Controllers
{
[Authorize]
public class AccountController : Controller
{
private ApplicationSignInManager _signInManager;
private ApplicationUserManager _userManager;
private readonly IAccount _accountObj;
public AccountController()
{
}
public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager, IAccount account)
{
UserManager = userManager;
SignInManager = signInManager;
_accountObj = account;
}
public ApplicationSignInManager SignInManager
{
get
{
return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
}
private set
{
_signInManager = value;
}
}
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
[AllowAnonymous]
public ActionResult Register()
{
return View();
}
//
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
AccountModel _accountModel = new AccountModel();
_accountModel.Id = user.Id;
_accountModel.Email = user.Email;
_accountModel.EmailConfirmed = user.EmailConfirmed;
_accountModel.PasswordHash = user.PasswordHash;
_accountModel.SecurityStamp = user.SecurityStamp;
_accountModel.PhoneNumber = user.PhoneNumber;
_accountModel.PhoneNumberConfirmed = user.PhoneNumberConfirmed;
_accountModel.TwoFactorEnabled = user.TwoFactorEnabled;
_accountModel.LockoutEndDateUtc = user.LockoutEndDateUtc;
_accountModel.LockoutEnabled = user.LockoutEnabled;
_accountModel.AccessFailedCount = user.AccessFailedCount;
_accountModel.UserName = user.Id;
if (_accountModel != null)
{
//Getting error here at "_accountObj"
await _accountObj.SaveAdmin(_accountModel);
}
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
}
}
错误图片,另请参阅图片:
I am trying to calling repository method with the help of Interface in controller because I am using dependency injection, but at this point _accountObj.SaveAdmin(_accountModel)
. I am getting error like this Object reference not set to an instance of an object - "_accountObj was null". I am not able to save user signup data into repository to database. Please help me to resolve my issue. I will also upload image of the code and error for your better understanding.
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Security;
using Inspinia_MVC5.Models;
using Domain.Models;
using Services.Interface;
namespace Inspinia_MVC5.Controllers
{
[Authorize]
public class AccountController : Controller
{
private ApplicationSignInManager _signInManager;
private ApplicationUserManager _userManager;
private readonly IAccount _accountObj;
public AccountController()
{
}
public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager, IAccount account)
{
UserManager = userManager;
SignInManager = signInManager;
_accountObj = account;
}
public ApplicationSignInManager SignInManager
{
get
{
return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
}
private set
{
_signInManager = value;
}
}
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
[AllowAnonymous]
public ActionResult Register()
{
return View();
}
//
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
AccountModel _accountModel = new AccountModel();
_accountModel.Id = user.Id;
_accountModel.Email = user.Email;
_accountModel.EmailConfirmed = user.EmailConfirmed;
_accountModel.PasswordHash = user.PasswordHash;
_accountModel.SecurityStamp = user.SecurityStamp;
_accountModel.PhoneNumber = user.PhoneNumber;
_accountModel.PhoneNumberConfirmed = user.PhoneNumberConfirmed;
_accountModel.TwoFactorEnabled = user.TwoFactorEnabled;
_accountModel.LockoutEndDateUtc = user.LockoutEndDateUtc;
_accountModel.LockoutEnabled = user.LockoutEnabled;
_accountModel.AccessFailedCount = user.AccessFailedCount;
_accountModel.UserName = user.Id;
if (_accountModel != null)
{
//Getting error here at "_accountObj"
await _accountObj.SaveAdmin(_accountModel);
}
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
}
}
Error image, please see the image also:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DLDR;删除带有 0 个参数的构造函数。
你有 2 个构造函数。在两者中都放置一个断点,然后调试代码以找出正在调用哪一个。我猜测它是默认构造函数(带有 0 个参数的构造函数),因此
UserManager
、SignInManager
和_accountObj
均为 null。DLDR; remove the constructor with 0 parameters.
You have 2 constructors. Put a breakpoint in both, and debug your code to find out which one is being called. I'm guessing it's the default constructor (the one with 0 parameters), and therefore
UserManager
,SignInManager
and_accountObj
are all null.