【问题标题】:Issue with launching controller after creating admin user创建管理员用户后启动控制器的问题
【发布时间】:2018-04-30 21:50:38
【问题描述】:

目前在创建管理员用户后我的控制器出现问题。 这是用于创建管理员用户的代码:

  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

【问题讨论】:

  • 需要调用base.Initialize()来初始化Controller基类。

标签: c# asp.net-mvc


【解决方案1】:

这个方法不能直接调用。按顺序覆盖此方法 在任何 ActionResult 方法之前提供额外的处理任务 被调用,例如设置线程文化或分配自定义 TempData 对象的提供程序。如果重写此方法,请调用 基本控件的 Initialize 方法。

来自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");
    }

}

【讨论】:

  • 现在它正在破坏 userManager.AddToRole(user.Id, "admin");
  • System.InvalidOperationException: '找不到用户 ID。'
  • 这是一个不同的问题。尝试先搜索问题:) stackoverflow.com/questions/24389126/…
  • @PJ.SQL 那是因为您没有应用我在回答您上一个问题时提供的代码
  • 所以还有一个问题:D
猜你喜欢
  • 2011-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-10
  • 2018-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多