我无法在WebApi Controller中实例化UserManager,我找到了各种答案here,here
也是。我已经尝试了所有,但仍然没有解决。
这是我的代码:
public class CampaignController : ApiController
{
private ApplicationUserManager _userManager;
private ApplicationSignInManager _signInManager;
public CampaignController()
{
}
public CampaignController(ApplicationUserManager userManager, ApplicationSignInManager signInManager)
{
UserManager = userManager;
SignInManager = signInManager;
}
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
public ApplicationSignInManager SignInManager
{
get
{
return _signInManager ?? HttpContext.Current.GetOwinContext().Get<ApplicationSignInManager>();
}
private set
{
_signInManager = value;
}
}
[HttpPost]
[Route("savesync")]
public object SaveSync(AsaanAccountCampaignInfo model)
{
try
{
var user = new ApplicationUser { UserName = model.MobileNumber, PhoneNumber = model.MobileNumber, Email = model.EmailAddress, VerificationCode = VerificationCode, CreatedBy = "Local System" };
var result = UserManager.Create(user, "default123");
if (result.Succeeded)
{
return new { status = true, message = "Record Added Succefully." };
}
}
catch (Exception e)
{
throw e;
}
}
}
它将在以下位置抛出Null引用异常: UserManager.Create(用户,“ default123”);