我在将模型对象传递给存储库方法时遇到错误(_accountObj )

发布于 2025-01-13 12:41:59 字数 4048 浏览 0 评论 0原文

我尝试在控制器中的接口的帮助下调用存储库方法,因为我正在使用依赖注入,但此时_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);
        }
    }
}

错误图片,另请参阅图片:

图片 1

图片 2

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:

Image 1

Image 2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十年九夏 2025-01-20 12:41:59

DLDR;删除带有 0 个参数的构造函数。

你有 2 个构造函数。在两者中都放置一个断点,然后调试代码以找出正在调用哪一个。我猜测它是默认构造函数(带有 0 个参数的构造函数),因此 UserManagerSignInManager_accountObj 均为 null。

    public AccountController()
    {
    }

    public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager, IAccount account)
    {
        UserManager = userManager;
        SignInManager = signInManager;
        _accountObj = account;
    }

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.

    public AccountController()
    {
    }

    public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager, IAccount account)
    {
        UserManager = userManager;
        SignInManager = signInManager;
        _accountObj = account;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文