创建管理员用户后,我的控制器出现问题。 这是用于创建管理员用户的代码:
protected override void Initialize(RequestContext requestContext)
{
using (var ac = new ApplicationDbContext())
{
var userManager = new ApplicationUserManager(new
UserStore<ApplicationUser>(ac));
var roleManager = new RoleManager<IdentityRole>(new
RoleStore<IdentityRole>(ac));
var user = new ApplicationUser { UserName = "hello@gmail.com",
Email = "hello@gmail.com" };
userManager.Create(user, "Hello1234!");
roleManager.Create(new IdentityRole("admin"));
user = userManager.FindByNameAsync(user.UserName).Result;
userManager.AddToRole(user.Id, "admin");
}
}
由于错误而更新
当我导航到产品控制器时,我在浏览器中收到此错误:
Server Error in '/' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.Mvc.Controller.PossiblyLoadTempData() +11
System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +39
System.Web.Mvc.Controller.<BeginExecute>b__14(AsyncCallback asyncCallback, Object callbackState, Controller controller) +18
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +20
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +374
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +16
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__4(AsyncCallback asyncCallback, Object asyncState, ProcessRequestState innerState) +52
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +384
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +103
System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +48
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +159
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.7.2633.0
答案 0 :(得分:2)
此方法无法直接调用。按顺序重写此方法 在任何ActionResult方法之前提供额外的处理任务 被调用,例如设置线程文化或分配自定义 TempData对象的提供者。如果重写此方法,请调用 基本控制的初始化方法。
来自MSDN
protected override void Initialize(RequestContext requestContext)
{
base.Initialize(requestContext);
using (var ac = new ApplicationDbContext())
{
var userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(ac));
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(ac));
var user = new ApplicationUser { UserName = "hello@gmail.com", Email = "hello@gmail.com"};
userManager.Create(user, "Hello1234!");
roleManager.Create(new IdentityRole("admin"));
user = userManager.FindByNameAsync(user.UserName).Result;
userManager.AddToRole(user.Id, "admin");
}
}